import React, { useState, useEffect, useRef, useCallback } from 'react';
import isEqual from 'lodash-es/isEqual';
import { faGear } from '@fortawesome/free-solid-svg-icons/faGear';
import { faSliders } from '@fortawesome/free-solid-svg-icons/faSliders';
import { WithButtonSwitcherSchedulerContainer } from './with-button-switcher-scheduler-container';
import { WithTabsSwitcherSchedulerContainer } from './with-tabs-switcher-scheduler-container';
import { DEFAULT_SCHEDULE, useDaysOfWeek, useMonths, useFrequencyOptions, SWITCHER_TYPES, useDefaultTexts } from './constants';
import { getFrequency, useArrayOfRangedNumbersOrAllValidator } from './utils';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * Scheduler is form for cron jobs configuration
 * @midnight-gray-supported
 * @alpine-light-supported
 */
export function Scheduler({
  onUpdateSchedule = () => {},
  initialSchedule = DEFAULT_SCHEDULE,
  testId = 'scheduler',
  isDisabled = false,
  frequencyOptions: frequencyOptionsParams,
  daysOfWeek: daysOfWeekParams,
  months: monthsParams,
  validateArrayOfRangedNumbersOrAll: validateArrayOfRangedNumbersOrAllParams,
  simpleSchedulerToggleButtonText: simpleSchedulerToggleButtonTextParams,
  advancedSchedulerToggleButtonText: advancedSchedulerToggleButtonTextParams,
  simpleSchedulerModalTexts: simpleSchedulerModalTextsParams,
  labelTexts: labelTextsParams,
  advancedSchedulerInfoTexts: advancedSchedulerInfoTextsParams,
  validationTexts: validationTextsParams,
  switcherType = SWITCHER_TYPES.BUTTON,
  selectedTabKey,
  tabTexts: tabTextsParams,
  advancedSchedulerSwitcherIcon = faGear,
  simpleSchedulerSwitcherIcon = faSliders
}) {
  // Get translated constants from hooks
  const defaultDaysOfWeek = useDaysOfWeek();
  const defaultValidateArrayOfRangedNumbersOrAll = useArrayOfRangedNumbersOrAllValidator();
  const defaultMonths = useMonths();
  const defaultFrequencyOptions = useFrequencyOptions();
  const months = monthsParams ?? defaultMonths;
  const daysOfWeek = daysOfWeekParams ?? defaultDaysOfWeek;
  const frequencyOptions = frequencyOptionsParams ?? defaultFrequencyOptions;
  const validateArrayOfRangedNumbersOrAll = validateArrayOfRangedNumbersOrAllParams ?? defaultValidateArrayOfRangedNumbersOrAll;
  const {
    defaultSimpleSchedulerToggleButtonText,
    defaultAdvancedSchedulerToggleButtonText,
    defaultSimpleSchedulerModalTexts,
    defaultLabelTexts,
    defaultAdvancedSchedulerInfoTexts,
    defaultValidationTexts,
    defaultTabTexts
  } = useDefaultTexts();

  // Use the provided values or defaults
  const simpleSchedulerToggleButtonText = simpleSchedulerToggleButtonTextParams ?? defaultSimpleSchedulerToggleButtonText;
  const advancedSchedulerToggleButtonText = advancedSchedulerToggleButtonTextParams ?? defaultAdvancedSchedulerToggleButtonText;
  const simpleSchedulerModalTexts = simpleSchedulerModalTextsParams ?? defaultSimpleSchedulerModalTexts;
  const labelTexts = labelTextsParams ?? defaultLabelTexts;
  const advancedSchedulerInfoTexts = advancedSchedulerInfoTextsParams ?? defaultAdvancedSchedulerInfoTexts;
  const validationTexts = validationTextsParams ?? defaultValidationTexts;
  const tabTexts = tabTextsParams ?? defaultTabTexts;
  const [showAdvancedScheduler, setShowAdvancedScheduler] = useState(false);
  const [schedulerKey, setSchedulerKey] = useState(`scheduler-${Date.now()}`);
  const schedule = useRef();
  useEffect(function () {
    // Check if the new schedule can be expressed in one of the simple cases
    const simpleSchedulerFrequency = getFrequency(initialSchedule);

    // Ensure the child component is remounted if the initial schedule changes
    if (!isEqual(schedule.current, initialSchedule)) {
      setSchedulerKey(`scheduler-${Date.now()}`);

      // If we are in simple mode, and the schedule is complex, switch to the advanced mode
      if (!showAdvancedScheduler && !simpleSchedulerFrequency) {
        setShowAdvancedScheduler(true);
      }
      // If we are in advanced mode, and the schedule is simple, switch to simple mode
      else if (showAdvancedScheduler && simpleSchedulerFrequency) {
        setShowAdvancedScheduler(false);
      }
    }
  }, [initialSchedule?.minute?.join(','), initialSchedule?.hour?.join(','), initialSchedule?.dayOfWeek?.join(','), initialSchedule?.dayOfMonth?.join(','), initialSchedule?.month?.join(',')]);
  const onUseSimpleScheduler = useCallback(() => {
    onUpdateSchedule(DEFAULT_SCHEDULE, null);
    setShowAdvancedScheduler(false);
  }, [setShowAdvancedScheduler, onUpdateSchedule]);
  function handleUpdateSchedule({
    newSchedule,
    errors = null
  }) {
    schedule.current = newSchedule;
    onUpdateSchedule(newSchedule, errors);
  }
  return /*#__PURE__*/_jsxs("div", {
    className: "scheduler",
    "test-id": testId,
    children: [switcherType === SWITCHER_TYPES.BUTTON && /*#__PURE__*/_jsx(WithButtonSwitcherSchedulerContainer, {
      showAdvancedScheduler: showAdvancedScheduler,
      onUpdateSchedule: handleUpdateSchedule,
      isDisabled: isDisabled,
      validationTexts: validationTexts,
      initialSchedule: initialSchedule,
      schedulerKey: schedulerKey,
      validateArrayOfRangedNumbersOrAll: validateArrayOfRangedNumbersOrAll,
      simpleSchedulerToggleButtonText: simpleSchedulerToggleButtonText,
      advancedSchedulerToggleButtonText: advancedSchedulerToggleButtonText,
      simpleSchedulerModalTexts: simpleSchedulerModalTexts,
      advancedSchedulerInfoTexts: advancedSchedulerInfoTexts,
      labelTexts: labelTexts,
      frequencyOptions: frequencyOptions,
      daysOfWeek: daysOfWeek,
      months: months,
      onUseSimpleScheduler: onUseSimpleScheduler,
      onUseAdvancedScheduler: () => {
        setShowAdvancedScheduler(true);
      },
      advancedSchedulerSwitcherIcon: advancedSchedulerSwitcherIcon,
      simpleSchedulerSwitcherIcon: simpleSchedulerSwitcherIcon
    }), switcherType === SWITCHER_TYPES.TABS && /*#__PURE__*/_jsx(WithTabsSwitcherSchedulerContainer, {
      onUpdateSchedule: handleUpdateSchedule,
      isDisabled: isDisabled,
      validationTexts: validationTexts,
      initialSchedule: initialSchedule,
      schedulerKey: schedulerKey,
      validateArrayOfRangedNumbersOrAll: validateArrayOfRangedNumbersOrAll,
      advancedSchedulerInfoTexts: advancedSchedulerInfoTexts,
      labelTexts: labelTexts,
      frequencyOptions: frequencyOptions,
      daysOfWeek: daysOfWeek,
      months: months,
      selectedTabKey: selectedTabKey,
      onUseSimpleScheduler: onUseSimpleScheduler,
      tabTexts: tabTexts
    })]
  });
}