"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.AdvancedScheduler = AdvancedScheduler;
var _react = _interopRequireWildcard(require("react"));
var _faCircleInfo = require("@fortawesome/free-solid-svg-icons/faCircleInfo");
var _input = require("../input");
var _constants = require("./constants");
var _utils = require("./utils");
var _jsxRuntime = require("react/jsx-runtime");
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); }
function AdvancedScheduler({
  onUpdateSchedule,
  initialSchedule,
  validateArrayOfRangedNumbersOrAll,
  labelTexts,
  infoTexts,
  isDisabled = false
}) {
  const [minute, setMinute] = (0, _react.useState)({
    value: initialSchedule?.minute?.join(','),
    validity: validateArrayOfRangedNumbersOrAll({
      ..._constants.FIELD_BOUNDARIES.minute,
      values: initialSchedule.minute
    })
  });
  const [hour, setHour] = (0, _react.useState)({
    value: initialSchedule?.hour?.join(','),
    validity: validateArrayOfRangedNumbersOrAll({
      ..._constants.FIELD_BOUNDARIES.hour,
      values: initialSchedule.hour
    })
  });
  const [dayOfMonth, setDayOfMonth] = (0, _react.useState)({
    value: initialSchedule?.dayOfMonth?.join(','),
    validity: validateArrayOfRangedNumbersOrAll({
      ..._constants.FIELD_BOUNDARIES.dayOfMonth,
      values: initialSchedule.dayOfMonth
    })
  });
  const [month, setMonth] = (0, _react.useState)({
    value: initialSchedule?.month?.join(','),
    validity: validateArrayOfRangedNumbersOrAll({
      ..._constants.FIELD_BOUNDARIES.month,
      values: initialSchedule.month
    })
  });
  const [dayOfWeek, setDayOfWeek] = (0, _react.useState)({
    value: initialSchedule?.dayOfWeek?.join(','),
    validity: validateArrayOfRangedNumbersOrAll({
      ..._constants.FIELD_BOUNDARIES.dayOfWeek,
      values: initialSchedule.dayOfWeek
    })
  });

  // Update the schedule upstream if no fields have validation errors
  (0, _react.useEffect)(function () {
    const result = {
      newSchedule: {
        minute: (0, _utils.stringToArrayOfNumbersOrAll)(minute.validity?.validValues ?? minute.value),
        hour: (0, _utils.stringToArrayOfNumbersOrAll)(hour.validity?.validValues ?? hour.value),
        dayOfMonth: (0, _utils.stringToArrayOfNumbersOrAll)(dayOfMonth.validity?.validValues ?? dayOfMonth.value),
        month: (0, _utils.stringToArrayOfNumbersOrAll)(month.validity?.validValues ?? month.value),
        dayOfWeek: (0, _utils.stringToArrayOfNumbersOrAll)(dayOfWeek.validity?.validValues ?? dayOfWeek.value)
      },
      errors: [{
        ...minute.validity,
        field: 'minute'
      }, {
        ...hour.validity,
        field: 'hour'
      }, {
        ...dayOfMonth.validity,
        field: 'dayOfMonth'
      }, {
        ...month.validity,
        field: 'month'
      }, {
        ...dayOfWeek.validity,
        field: 'dayOfWeek'
      }].filter(item => item.type) // remove any fields without error
    };
    onUpdateSchedule(result);
  }, [minute, hour, dayOfMonth, month, dayOfWeek]);
  function handleFieldUpdate({
    value,
    min,
    max,
    setter
  }) {
    const values = value.split(',').filter(item => item);
    const validity = validateArrayOfRangedNumbersOrAll({
      min,
      max,
      values
    });
    setter({
      value,
      validity
    });
  }
  const INPUTS = [{
    id: 'minute',
    label: labelTexts.minute,
    boundaries: _constants.FIELD_BOUNDARIES.minute,
    stateValue: minute,
    stateSetter: setMinute,
    infoText: infoTexts.minute
  }, {
    id: 'hour',
    label: labelTexts.hour,
    boundaries: _constants.FIELD_BOUNDARIES.hour,
    stateValue: hour,
    stateSetter: setHour,
    infoText: infoTexts.hour
  }, {
    id: 'day-of-month',
    label: labelTexts.dayOfMonth,
    boundaries: _constants.FIELD_BOUNDARIES.dayOfMonth,
    stateValue: dayOfMonth,
    stateSetter: setDayOfMonth,
    infoText: infoTexts.dayOfMonth
  }, {
    id: 'month',
    label: labelTexts.month,
    boundaries: _constants.FIELD_BOUNDARIES.month,
    stateValue: month,
    stateSetter: setMonth,
    infoText: infoTexts.month
  }, {
    id: 'day-of-week',
    label: labelTexts.dayOfWeek,
    boundaries: _constants.FIELD_BOUNDARIES.dayOfWeek,
    stateValue: dayOfWeek,
    stateSetter: setDayOfWeek,
    infoText: infoTexts.dayOfWeek
  }];
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    className: "advanced-scheduler-form",
    "test-id": "advanced-scheduler",
    children: INPUTS.map(input => /*#__PURE__*/(0, _jsxRuntime.jsx)(_input.Input, {
      id: input.id,
      name: input.id,
      "test-id": `${input.id}-input`,
      label: input.label,
      labelTestId: `${input.id}-label`,
      onChange: e => {
        handleFieldUpdate({
          ...input.boundaries,
          value: e.target.value,
          setter: input.stateSetter
        });
      },
      value: input.stateValue.value,
      validity: input.stateValue.validity,
      validationTestId: `${input.id}-validation`,
      isDisabled: isDisabled,
      ...(input.infoText ? {
        labelIcon: _faCircleInfo.faCircleInfo,
        labelIconTooltipText: input.infoText,
        labelIconTestId: `${input.id}-label-info-icon`
      } : {})
    }, input.id))
  });
}