function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
import React, { useContext, useEffect, useMemo } from 'react';
import { Checkbox, Form, InputNumber, Radio, Select } from 'antd';
import { locales } from '@pisell/utils';
import { settingsContext } from "./index";
var CorrectionTypesField = function CorrectionTypesField(props) {
  var _props$value;
  var value = (_props$value = props.value) !== null && _props$value !== void 0 ? _props$value : [];
  var items = useMemo(function () {
    return [{
      key: 'missing',
      titleKey: 'pisell2.attendance.group-settings.correction-policy.types.missing.title',
      descKey: 'pisell2.attendance.group-settings.correction-policy.types.missing.desc'
    }, {
      key: 'late',
      titleKey: 'pisell2.attendance.group-settings.correction-policy.types.late.title',
      descKey: 'pisell2.attendance.group-settings.correction-policy.types.late.desc'
    }, {
      key: 'early_leave',
      titleKey: 'pisell2.attendance.group-settings.correction-policy.types.early-out.title',
      descKey: 'pisell2.attendance.group-settings.correction-policy.types.early-out.desc'
    }, {
      key: 'normal',
      titleKey: 'pisell2.attendance.group-settings.correction-policy.types.normal.title',
      descKey: 'pisell2.attendance.group-settings.correction-policy.types.normal.desc'
    }];
  }, []);
  return /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__check-cards"
  }, items.map(function (item) {
    var checked = value.includes(item.key);
    return /*#__PURE__*/React.createElement("div", {
      key: item.key,
      className: ['saas-attendance-group-settings__check-card', checked ? 'saas-attendance-group-settings__check-card--checked' : ''].join(' '),
      onClick: function onClick() {
        var _props$onChange;
        var next = checked ? value.filter(function (x) {
          return x !== item.key;
        }) : [].concat(_toConsumableArray(value), [item.key]);
        (_props$onChange = props.onChange) === null || _props$onChange === void 0 || _props$onChange.call(props, next);
      }
    }, /*#__PURE__*/React.createElement(Checkbox, {
      checked: checked,
      onChange: function onChange() {}
    }), /*#__PURE__*/React.createElement("div", {
      className: "saas-attendance-group-settings__check-content"
    }, /*#__PURE__*/React.createElement("div", {
      className: "saas-attendance-group-settings__check-title"
    }, locales.getText(item.titleKey)), /*#__PURE__*/React.createElement("div", {
      className: "saas-attendance-group-settings__check-desc"
    }, locales.getText(item.descKey))));
  }));
};
var CorrectionTimeLimitField = function CorrectionTimeLimitField(props) {
  return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__inline-input"
  }, /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__inline-text"
  }, locales.getText('pisell2.attendance.group-settings.correction-policy.time-limit.request-past-days.prefix')), /*#__PURE__*/React.createElement(InputNumber, {
    min: 0,
    max: 180,
    precision: 0,
    step: 1,
    value: props.value,
    onChange: props.onChange
  }), /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__inline-text"
  }, locales.getText('pisell2.attendance.group-settings.correction-policy.time-limit.request-past-days.suffix'))), /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__help"
  }, locales.getText('pisell2.attendance.group-settings.correction-policy.time-limit.helper')));
};
var CorrectionMonthlyLimitField = function CorrectionMonthlyLimitField(props) {
  return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__inline-input"
  }, /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__inline-text"
  }, locales.getText('pisell2.attendance.group-settings.correction-policy.monthly-limit.maximum.prefix')), /*#__PURE__*/React.createElement(InputNumber, {
    min: 0,
    max: 999,
    precision: 0,
    step: 1,
    value: props.value,
    onChange: props.onChange
  }), /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__inline-text"
  }, locales.getText('pisell2.attendance.group-settings.correction-policy.monthly-limit.maximum.suffix'))), /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__help"
  }, locales.getText('pisell2.attendance.group-settings.correction-policy.monthly-limit.helper')));
};
var CorrectionResetPeriodField = function CorrectionResetPeriodField(props) {
  var _props$value2;
  var value = (_props$value2 = props.value) !== null && _props$value2 !== void 0 ? _props$value2 : 'natural_month';
  return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Radio.Group, {
    className: "saas-attendance-group-settings__radio-cards",
    value: value,
    onChange: function onChange(e) {
      var _e$target, _props$onChange2;
      var nextValue = e === null || e === void 0 || (_e$target = e.target) === null || _e$target === void 0 ? void 0 : _e$target.value;
      (_props$onChange2 = props.onChange) === null || _props$onChange2 === void 0 || _props$onChange2.call(props, nextValue);
    }
  }, /*#__PURE__*/React.createElement("label", {
    className: ['saas-attendance-group-settings__radio-card', value === 'natural_month' ? 'saas-attendance-group-settings__radio-card--selected' : ''].join(' ')
  }, /*#__PURE__*/React.createElement(Radio, {
    value: "natural_month"
  }), /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__radio-content"
  }, /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__radio-title"
  }, locales.getText('pisell2.attendance.group-settings.correction-policy.reset-period.calendar-month.title')), /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__radio-desc"
  }, locales.getText('pisell2.attendance.group-settings.correction-policy.reset-period.calendar-month.desc')))), /*#__PURE__*/React.createElement("label", {
    className: ['saas-attendance-group-settings__radio-card', value === 'custom' ? 'saas-attendance-group-settings__radio-card--selected' : ''].join(' ')
  }, /*#__PURE__*/React.createElement(Radio, {
    value: "custom"
  }), /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__radio-content"
  }, /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__radio-title"
  }, locales.getText('pisell2.attendance.group-settings.correction-policy.reset-period.custom-period.title')), /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__radio-desc"
  }, locales.getText('pisell2.attendance.group-settings.correction-policy.reset-period.custom-period.desc'))))));
};
var CorrectionPolicyTab = function CorrectionPolicyTab(props) {
  var _Form$useWatch, _Form$useWatch2;
  var form = props.form;
  var _useContext = useContext(settingsContext),
    members = _useContext.members;
  useEffect(function () {
    if (Array.isArray(members)) {
      var founder = members.find(function (item) {
        return item.type === 'founder';
      });
      var originApproverIds = form.getFieldValue(['correction', 'approver_ids']);
      if ((!Array.isArray(originApproverIds) || originApproverIds.length === 0) && founder) {
        form.setFieldValue(['correction', 'approver_ids'], [founder.id]);
      }
    }
  }, [members, form]);
  var approverOptions = useMemo(function () {
    if (!Array.isArray(members)) return [];
    return members.map(function (item) {
      return {
        label: item.nickname,
        value: item.id
      };
    });
  }, [members]);
  var customPeriodOptions = useMemo(function () {
    return Array.from({
      length: 28
    }, function (_, index) {
      var day = index + 1;
      return {
        label: String(day),
        value: day
      };
    });
  }, []);
  var allow = (_Form$useWatch = Form.useWatch(['correction', 'allow'], form)) !== null && _Form$useWatch !== void 0 ? _Form$useWatch : true;
  var period_type = (_Form$useWatch2 = Form.useWatch(['correction', 'period_type'], form)) !== null && _Form$useWatch2 !== void 0 ? _Form$useWatch2 : 'natural_month';
  useEffect(function () {
    var _form$getFieldValue;
    var correction = (_form$getFieldValue = form.getFieldValue(['correction'])) !== null && _form$getFieldValue !== void 0 ? _form$getFieldValue : {};
    var nextCorrection = {};
    if (typeof correction.allow === 'undefined') nextCorrection.allow = true;
    if (!Array.isArray(correction.types) || correction.types.length === 0) {
      nextCorrection.types = ['missing', 'late'];
    }
    if (!Array.isArray(correction.approver_ids)) nextCorrection.approver_ids = [];
    if (correction.days_limit === null || typeof correction.days_limit === 'undefined') {
      nextCorrection.days_limit = 7;
    }
    if (correction.monthly_times === null || typeof correction.monthly_times === 'undefined') {
      nextCorrection.monthly_times = 3;
    }
    if (typeof correction.period_type === 'undefined') {
      nextCorrection.period_type = 'natural_month';
    }
    if (typeof correction.period_custom === 'undefined') {
      var _ref, _correction$period_ty;
      var nextPeriodType = (_ref = (_correction$period_ty = correction.period_type) !== null && _correction$period_ty !== void 0 ? _correction$period_ty : nextCorrection.period_type) !== null && _ref !== void 0 ? _ref : 'natural_month';
      nextCorrection.period_custom = nextPeriodType === 'custom' ? 1 : null;
    }
    if (Object.keys(nextCorrection).length > 0) {
      form.setFieldsValue({
        correction: _objectSpread(_objectSpread({}, correction), nextCorrection)
      });
    }
  }, [form]);
  useEffect(function () {
    if (period_type !== 'custom') {
      form.setFieldValue(['correction', 'period_custom'], null);
    } else {
      var v = form.getFieldValue(['correction', 'period_custom']);
      if (v === null || typeof v === 'undefined') {
        form.setFieldValue(['correction', 'period_custom'], 1);
      }
    }
  }, [period_type, form]);
  return /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__panel",
    id: props.id
  }, /*#__PURE__*/React.createElement(Form.Item, {
    name: ['correction', 'allow'],
    valuePropName: "checked",
    initialValue: true
  }, /*#__PURE__*/React.createElement(Checkbox, {
    className: "saas-attendance-group-settings__allow-corrections",
    style: {
      width: '100%'
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__allow-title"
  }, locales.getText('pisell2.attendance.group-settings.correction-policy.allow-corrections.title')), /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__allow-desc"
  }, locales.getText('pisell2.attendance.group-settings.correction-policy.allow-corrections.desc')))), allow && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__section"
  }, /*#__PURE__*/React.createElement(Form.Item, {
    name: ['correction', 'types'],
    initialValue: ['missing', 'late'],
    label: locales.getText('pisell2.attendance.group-settings.correction-policy.types.label'),
    rules: [{
      required: true,
      message: locales.getText('pisell2.attendance.group-settings.correction-policy.types.label')
    }]
  }, /*#__PURE__*/React.createElement(CorrectionTypesField, null))), /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__section"
  }, /*#__PURE__*/React.createElement(Form.Item, {
    name: ['correction', 'approver_ids'],
    initialValue: [],
    label: locales.getText('pisell2.attendance.group-settings.correction-policy.approver.label'),
    rules: [{
      required: true,
      message: locales.getText('pisell2.attendance.group-settings.correction-policy.approver.required')
    }]
  }, /*#__PURE__*/React.createElement(Select, {
    allowClear: true,
    mode: "multiple",
    options: approverOptions,
    placeholder: locales.getText('pisell2.attendance.group-settings.correction-policy.approver.placeholder'),
    showSearch: true,
    optionFilterProp: "label"
  }))), /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__section"
  }, /*#__PURE__*/React.createElement(Form.Item, {
    name: ['correction', 'days_limit'],
    initialValue: 7,
    label: locales.getText('pisell2.attendance.group-settings.correction-policy.time-limit.label'),
    rules: [{
      required: true,
      message: locales.getText('pisell2.attendance.group-settings.correction-policy.time-limit.required')
    }]
  }, /*#__PURE__*/React.createElement(CorrectionTimeLimitField, null))), /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__section"
  }, /*#__PURE__*/React.createElement(Form.Item, {
    name: ['correction', 'monthly_times'],
    initialValue: 3,
    label: locales.getText('pisell2.attendance.group-settings.correction-policy.monthly-limit.label'),
    rules: [{
      required: true,
      message: locales.getText('pisell2.attendance.group-settings.correction-policy.monthly-limit.required')
    }]
  }, /*#__PURE__*/React.createElement(CorrectionMonthlyLimitField, null))), /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__section"
  }, /*#__PURE__*/React.createElement(Form.Item, {
    name: ['correction', 'period_type'],
    initialValue: "natural_month",
    label: locales.getText('pisell2.attendance.group-settings.correction-policy.reset-period.label')
  }, /*#__PURE__*/React.createElement(CorrectionResetPeriodField, null))), period_type === 'custom' && /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__section"
  }, /*#__PURE__*/React.createElement(Form.Item, {
    name: ['correction', 'period_custom'],
    initialValue: 1,
    rules: [{
      required: true,
      message: locales.getText('pisell2.attendance.group-settings.correction-policy.reset-period.custom-period.required')
    }]
  }, /*#__PURE__*/React.createElement(Select, {
    options: customPeriodOptions
  })))));
};
export default CorrectionPolicyTab;