/**
 * Common form handling utilities
 */

/**
 * Generic form change handler
 * @param {function} setState - State setter function
 * @returns {function} - Change handler
 */
export const createChangeHandler = (setState) => (e) => {
  setState(prev => ({ ...prev, [e.target.name]: e.target.value }));
};

/**
 * Generic toggle handler for switches
 * @param {function} setState - State setter function
 * @param {string} fieldName - Field name to update
 * @returns {function} - Toggle handler
 */
export const createToggleHandler = (setState, fieldName) => (value) => {
  setState(prev => ({ ...prev, [fieldName]: value }));
};

/**
 * Generic checkbox handler
 * @param {function} setState - State setter function
 * @param {string} fieldName - Field name to update
 * @returns {function} - Checkbox handler
 */
export const createCheckboxHandler = (setState, fieldName) => (e) => {
  const isChecked = e.target.checked;
  setState(prev => ({ ...prev, [fieldName]: isChecked }));
};

/**
 * Generic select handler with integer parsing
 * @param {function} setState - State setter function
 * @param {string} fieldName - Field name to update
 * @returns {function} - Select handler
 */
export const createSelectHandler = (setState, fieldName) => (e) => {
  const selectedValue = parseInt(e.target.value, 10);
  setState(prev => ({ ...prev, [fieldName]: selectedValue }));
};

/**
 * Create premium feature handler
 * @param {function} setState - State setter function
 * @param {string} fieldName - Field name to update
 * @param {boolean} isPremium - Whether feature is premium
 * @param {function} showPremiumModal - Function to show premium modal
 * @returns {function} - Premium feature handler
 */
export const createPremiumToggleHandler = (setState, fieldName, isPremium, showPremiumModal) => (value) => {
  if (isPremium) {
    showPremiumModal();
    return;
  }
  setState(prev => ({ ...prev, [fieldName]: value }));
};

/**
 * Create interval options for select dropdowns
 * @param {boolean} includePremium - Whether to include premium options
 * @returns {Array} - Array of interval options
 */
export const createIntervalOptions = (includePremium = false) => {
  const baseOptions = [
    { value: 60, label: "1 min interval" },
    { value: 180, label: "3 min" },
    { value: 300, label: "5 min" },
    { value: 18000, label: "5 hours" }
  ];

  const premiumOptions = [
    { value: 1800, label: "30 min" },
    { value: 3600, label: "1 hour" },
    { value: 36000, label: "10 hours" },
    { value: 54000, label: "15 hours" },
    { value: 64800, label: "18 hours" },
    { value: 86400, label: "24 hours" },
    { value: 172800, label: "2 days" },
    { value: 259200, label: "3 days" },
    { value: 604800, label: "7 days" }
  ];

  return includePremium ? [...baseOptions, ...premiumOptions] : baseOptions;
};

/**
 * Create days options for select dropdowns
 * @param {boolean} includePremium - Whether to include premium options
 * @returns {Array} - Array of days options
 */
export const createDaysOptions = (includePremium = false) => {
  const baseOptions = [
    { value: -8, label: "8 days" },
    { value: -9, label: "9 days" },
    { value: -10, label: "10 days" },
    { value: -15, label: "15 days" },
    { value: -20, label: "20 days" },
    { value: -60, label: "Last 2 month" },
    { value: -30, label: "30 days" }
  ];

  const premiumOptions = [
    { value: -1, label: "From last 1 days" },
    { value: -2, label: "last 2 days" },
    { value: -3, label: "3 days" },
    { value: -4, label: "4 days" },
    { value: -5, label: "5 days" },
    { value: -7, label: "7 days" }
  ];

  return includePremium ? [...baseOptions, ...premiumOptions] : baseOptions;
};