var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/saasAttendance/settings/CorrectionPolicyTab.tsx
var CorrectionPolicyTab_exports = {};
__export(CorrectionPolicyTab_exports, {
  default: () => CorrectionPolicyTab_default
});
module.exports = __toCommonJS(CorrectionPolicyTab_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_utils = require("@pisell/utils");
var import_index = require("./index");
var CorrectionTypesField = (props) => {
  const value = props.value ?? [];
  const items = (0, import_react.useMemo)(
    () => [
      {
        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__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__check-cards" }, items.map((item) => {
    const checked = value.includes(item.key);
    return /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        key: item.key,
        className: [
          "saas-attendance-group-settings__check-card",
          checked ? "saas-attendance-group-settings__check-card--checked" : ""
        ].join(" "),
        onClick: () => {
          var _a;
          const next = checked ? value.filter((x) => x !== item.key) : [...value, item.key];
          (_a = props.onChange) == null ? void 0 : _a.call(props, next);
        }
      },
      /* @__PURE__ */ import_react.default.createElement(import_antd.Checkbox, { checked, onChange: () => {
      } }),
      /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__check-content" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__check-title" }, import_utils.locales.getText(item.titleKey)), /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__check-desc" }, import_utils.locales.getText(item.descKey)))
    );
  }));
};
var CorrectionTimeLimitField = (props) => /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__inline-input" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__inline-text" }, import_utils.locales.getText(
  "pisell2.attendance.group-settings.correction-policy.time-limit.request-past-days.prefix"
)), /* @__PURE__ */ import_react.default.createElement(
  import_antd.InputNumber,
  {
    min: 0,
    max: 180,
    precision: 0,
    step: 1,
    value: props.value,
    onChange: props.onChange
  }
), /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__inline-text" }, import_utils.locales.getText(
  "pisell2.attendance.group-settings.correction-policy.time-limit.request-past-days.suffix"
))), /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__help" }, import_utils.locales.getText(
  "pisell2.attendance.group-settings.correction-policy.time-limit.helper"
)));
var CorrectionMonthlyLimitField = (props) => /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__inline-input" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__inline-text" }, import_utils.locales.getText(
  "pisell2.attendance.group-settings.correction-policy.monthly-limit.maximum.prefix"
)), /* @__PURE__ */ import_react.default.createElement(
  import_antd.InputNumber,
  {
    min: 0,
    max: 999,
    precision: 0,
    step: 1,
    value: props.value,
    onChange: props.onChange
  }
), /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__inline-text" }, import_utils.locales.getText(
  "pisell2.attendance.group-settings.correction-policy.monthly-limit.maximum.suffix"
))), /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__help" }, import_utils.locales.getText(
  "pisell2.attendance.group-settings.correction-policy.monthly-limit.helper"
)));
var CorrectionResetPeriodField = (props) => {
  const value = props.value ?? "natural_month";
  return /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Radio.Group,
    {
      className: "saas-attendance-group-settings__radio-cards",
      value,
      onChange: (e) => {
        var _a, _b;
        const nextValue = (_a = e == null ? void 0 : e.target) == null ? void 0 : _a.value;
        (_b = props.onChange) == null ? void 0 : _b.call(props, nextValue);
      }
    },
    /* @__PURE__ */ import_react.default.createElement(
      "label",
      {
        className: [
          "saas-attendance-group-settings__radio-card",
          value === "natural_month" ? "saas-attendance-group-settings__radio-card--selected" : ""
        ].join(" ")
      },
      /* @__PURE__ */ import_react.default.createElement(import_antd.Radio, { value: "natural_month" }),
      /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__radio-content" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__radio-title" }, import_utils.locales.getText(
        "pisell2.attendance.group-settings.correction-policy.reset-period.calendar-month.title"
      )), /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__radio-desc" }, import_utils.locales.getText(
        "pisell2.attendance.group-settings.correction-policy.reset-period.calendar-month.desc"
      )))
    ),
    /* @__PURE__ */ import_react.default.createElement(
      "label",
      {
        className: [
          "saas-attendance-group-settings__radio-card",
          value === "custom" ? "saas-attendance-group-settings__radio-card--selected" : ""
        ].join(" ")
      },
      /* @__PURE__ */ import_react.default.createElement(import_antd.Radio, { value: "custom" }),
      /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__radio-content" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__radio-title" }, import_utils.locales.getText(
        "pisell2.attendance.group-settings.correction-policy.reset-period.custom-period.title"
      )), /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__radio-desc" }, import_utils.locales.getText(
        "pisell2.attendance.group-settings.correction-policy.reset-period.custom-period.desc"
      )))
    )
  ));
};
var CorrectionPolicyTab = (props) => {
  const { form } = props;
  const { members } = (0, import_react.useContext)(import_index.settingsContext);
  (0, import_react.useEffect)(() => {
    if (Array.isArray(members)) {
      const founder = members.find((item) => item.type === "founder");
      const originApproverIds = form.getFieldValue([
        "correction",
        "approver_ids"
      ]);
      if ((!Array.isArray(originApproverIds) || originApproverIds.length === 0) && founder) {
        form.setFieldValue(["correction", "approver_ids"], [founder.id]);
      }
    }
  }, [members, form]);
  const approverOptions = (0, import_react.useMemo)(() => {
    if (!Array.isArray(members)) return [];
    return members.map((item) => ({
      label: item.nickname,
      value: item.id
    }));
  }, [members]);
  const customPeriodOptions = (0, import_react.useMemo)(
    () => Array.from({ length: 28 }, (_, index) => {
      const day = index + 1;
      return { label: String(day), value: day };
    }),
    []
  );
  const allow = import_antd.Form.useWatch(["correction", "allow"], form) ?? true;
  const period_type = import_antd.Form.useWatch(["correction", "period_type"], form) ?? "natural_month";
  (0, import_react.useEffect)(() => {
    const correction = form.getFieldValue(["correction"]) ?? {};
    const 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") {
      const nextPeriodType = correction.period_type ?? nextCorrection.period_type ?? "natural_month";
      nextCorrection.period_custom = nextPeriodType === "custom" ? 1 : null;
    }
    if (Object.keys(nextCorrection).length > 0) {
      form.setFieldsValue({
        correction: {
          ...correction,
          ...nextCorrection
        }
      });
    }
  }, [form]);
  (0, import_react.useEffect)(() => {
    if (period_type !== "custom") {
      form.setFieldValue(["correction", "period_custom"], null);
    } else {
      const v = form.getFieldValue(["correction", "period_custom"]);
      if (v === null || typeof v === "undefined") {
        form.setFieldValue(["correction", "period_custom"], 1);
      }
    }
  }, [period_type, form]);
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__panel", id: props.id }, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Form.Item,
    {
      name: ["correction", "allow"],
      valuePropName: "checked",
      initialValue: true
    },
    /* @__PURE__ */ import_react.default.createElement(
      import_antd.Checkbox,
      {
        className: "saas-attendance-group-settings__allow-corrections",
        style: { width: "100%" }
      },
      /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__allow-title" }, import_utils.locales.getText(
        "pisell2.attendance.group-settings.correction-policy.allow-corrections.title"
      )),
      /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__allow-desc" }, import_utils.locales.getText(
        "pisell2.attendance.group-settings.correction-policy.allow-corrections.desc"
      ))
    )
  ), allow && /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__section" }, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Form.Item,
    {
      name: ["correction", "types"],
      initialValue: ["missing", "late"],
      label: import_utils.locales.getText(
        "pisell2.attendance.group-settings.correction-policy.types.label"
      ),
      rules: [
        {
          required: true,
          message: import_utils.locales.getText(
            "pisell2.attendance.group-settings.correction-policy.types.label"
          )
        }
      ]
    },
    /* @__PURE__ */ import_react.default.createElement(CorrectionTypesField, null)
  )), /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__section" }, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Form.Item,
    {
      name: ["correction", "approver_ids"],
      initialValue: [],
      label: import_utils.locales.getText(
        "pisell2.attendance.group-settings.correction-policy.approver.label"
      ),
      rules: [
        {
          required: true,
          message: import_utils.locales.getText(
            "pisell2.attendance.group-settings.correction-policy.approver.required"
          )
        }
      ]
    },
    /* @__PURE__ */ import_react.default.createElement(
      import_antd.Select,
      {
        allowClear: true,
        mode: "multiple",
        options: approverOptions,
        placeholder: import_utils.locales.getText(
          "pisell2.attendance.group-settings.correction-policy.approver.placeholder"
        ),
        showSearch: true,
        optionFilterProp: "label"
      }
    )
  )), /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__section" }, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Form.Item,
    {
      name: ["correction", "days_limit"],
      initialValue: 7,
      label: import_utils.locales.getText(
        "pisell2.attendance.group-settings.correction-policy.time-limit.label"
      ),
      rules: [
        {
          required: true,
          message: import_utils.locales.getText(
            "pisell2.attendance.group-settings.correction-policy.time-limit.required"
          )
        }
      ]
    },
    /* @__PURE__ */ import_react.default.createElement(CorrectionTimeLimitField, null)
  )), /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__section" }, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Form.Item,
    {
      name: ["correction", "monthly_times"],
      initialValue: 3,
      label: import_utils.locales.getText(
        "pisell2.attendance.group-settings.correction-policy.monthly-limit.label"
      ),
      rules: [
        {
          required: true,
          message: import_utils.locales.getText(
            "pisell2.attendance.group-settings.correction-policy.monthly-limit.required"
          )
        }
      ]
    },
    /* @__PURE__ */ import_react.default.createElement(CorrectionMonthlyLimitField, null)
  )), /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__section" }, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Form.Item,
    {
      name: ["correction", "period_type"],
      initialValue: "natural_month",
      label: import_utils.locales.getText(
        "pisell2.attendance.group-settings.correction-policy.reset-period.label"
      )
    },
    /* @__PURE__ */ import_react.default.createElement(CorrectionResetPeriodField, null)
  )), period_type === "custom" && /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__section" }, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Form.Item,
    {
      name: ["correction", "period_custom"],
      initialValue: 1,
      rules: [
        {
          required: true,
          message: import_utils.locales.getText(
            "pisell2.attendance.group-settings.correction-policy.reset-period.custom-period.required"
          )
        }
      ]
    },
    /* @__PURE__ */ import_react.default.createElement(import_antd.Select, { options: customPeriodOptions })
  ))));
};
var CorrectionPolicyTab_default = CorrectionPolicyTab;
