"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Scheduler = Scheduler;
var _react = _interopRequireWildcard(require("react"));
var _isEqual = _interopRequireDefault(require("lodash-es/isEqual"));
var _faGear = require("@fortawesome/free-solid-svg-icons/faGear");
var _faSliders = require("@fortawesome/free-solid-svg-icons/faSliders");
var _withButtonSwitcherSchedulerContainer = require("./with-button-switcher-scheduler-container");
var _withTabsSwitcherSchedulerContainer = require("./with-tabs-switcher-scheduler-container");
var _constants = require("./constants");
var _utils = require("./utils");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
/**
 * Scheduler is form for cron jobs configuration
 * @midnight-gray-supported
 * @alpine-light-supported
 */
function Scheduler({
  onUpdateSchedule = () => {},
  initialSchedule = _constants.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 = _constants.SWITCHER_TYPES.BUTTON,
  selectedTabKey,
  tabTexts: tabTextsParams,
  advancedSchedulerSwitcherIcon = _faGear.faGear,
  simpleSchedulerSwitcherIcon = _faSliders.faSliders
}) {
  // Get translated constants from hooks
  const defaultDaysOfWeek = (0, _constants.useDaysOfWeek)();
  const defaultValidateArrayOfRangedNumbersOrAll = (0, _utils.useArrayOfRangedNumbersOrAllValidator)();
  const defaultMonths = (0, _constants.useMonths)();
  const defaultFrequencyOptions = (0, _constants.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
  } = (0, _constants.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] = (0, _react.useState)(false);
  const [schedulerKey, setSchedulerKey] = (0, _react.useState)(`scheduler-${Date.now()}`);
  const schedule = (0, _react.useRef)();
  (0, _react.useEffect)(function () {
    // Check if the new schedule can be expressed in one of the simple cases
    const simpleSchedulerFrequency = (0, _utils.getFrequency)(initialSchedule);

    // Ensure the child component is remounted if the initial schedule changes
    if (!(0, _isEqual.default)(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 = (0, _react.useCallback)(() => {
    onUpdateSchedule(_constants.DEFAULT_SCHEDULE, null);
    setShowAdvancedScheduler(false);
  }, [setShowAdvancedScheduler, onUpdateSchedule]);
  function handleUpdateSchedule({
    newSchedule,
    errors = null
  }) {
    schedule.current = newSchedule;
    onUpdateSchedule(newSchedule, errors);
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: "scheduler",
    "test-id": testId,
    children: [switcherType === _constants.SWITCHER_TYPES.BUTTON && /*#__PURE__*/(0, _jsxRuntime.jsx)(_withButtonSwitcherSchedulerContainer.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 === _constants.SWITCHER_TYPES.TABS && /*#__PURE__*/(0, _jsxRuntime.jsx)(_withTabsSwitcherSchedulerContainer.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
    })]
  });
}