const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_index = require("./index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
//#region src/plus/saasAttendance/settings/CorrectionPolicyTab.tsx
const CorrectionTypesField = (props) => {
	var _props$value;
	const value = (_props$value = props.value) !== null && _props$value !== void 0 ? _props$value : [];
	const items = (0, 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__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__check-cards" }, items.map((item) => {
		const checked = value.includes(item.key);
		return /* @__PURE__ */ 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 _props$onChange;
				const next = checked ? value.filter((x) => x !== item.key) : [...value, item.key];
				(_props$onChange = props.onChange) === null || _props$onChange === void 0 || _props$onChange.call(props, next);
			}
		}, /* @__PURE__ */ react.default.createElement(antd.Checkbox, {
			checked,
			onChange: () => {}
		}), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__check-content" }, /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__check-title" }, _pisell_utils.locales.getText(item.titleKey)), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__check-desc" }, _pisell_utils.locales.getText(item.descKey))));
	}));
};
const CorrectionTimeLimitField = (props) => /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__inline-input" }, /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__inline-text" }, _pisell_utils.locales.getText("pisell2.attendance.group-settings.correction-policy.time-limit.request-past-days.prefix")), /* @__PURE__ */ react.default.createElement(antd.InputNumber, {
	min: 0,
	max: 180,
	precision: 0,
	step: 1,
	value: props.value,
	onChange: props.onChange
}), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__inline-text" }, _pisell_utils.locales.getText("pisell2.attendance.group-settings.correction-policy.time-limit.request-past-days.suffix"))), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__help" }, _pisell_utils.locales.getText("pisell2.attendance.group-settings.correction-policy.time-limit.helper")));
const CorrectionMonthlyLimitField = (props) => /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__inline-input" }, /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__inline-text" }, _pisell_utils.locales.getText("pisell2.attendance.group-settings.correction-policy.monthly-limit.maximum.prefix")), /* @__PURE__ */ react.default.createElement(antd.InputNumber, {
	min: 0,
	max: 999,
	precision: 0,
	step: 1,
	value: props.value,
	onChange: props.onChange
}), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__inline-text" }, _pisell_utils.locales.getText("pisell2.attendance.group-settings.correction-policy.monthly-limit.maximum.suffix"))), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__help" }, _pisell_utils.locales.getText("pisell2.attendance.group-settings.correction-policy.monthly-limit.helper")));
const CorrectionResetPeriodField = (props) => {
	var _props$value2;
	const value = (_props$value2 = props.value) !== null && _props$value2 !== void 0 ? _props$value2 : "natural_month";
	return /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement(antd.Radio.Group, {
		className: "saas-attendance-group-settings__radio-cards",
		value,
		onChange: (e) => {
			var _e$target, _props$onChange2;
			const 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.default.createElement("label", { className: ["saas-attendance-group-settings__radio-card", value === "natural_month" ? "saas-attendance-group-settings__radio-card--selected" : ""].join(" ") }, /* @__PURE__ */ react.default.createElement(antd.Radio, { value: "natural_month" }), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__radio-content" }, /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__radio-title" }, _pisell_utils.locales.getText("pisell2.attendance.group-settings.correction-policy.reset-period.calendar-month.title")), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__radio-desc" }, _pisell_utils.locales.getText("pisell2.attendance.group-settings.correction-policy.reset-period.calendar-month.desc")))), /* @__PURE__ */ react.default.createElement("label", { className: ["saas-attendance-group-settings__radio-card", value === "custom" ? "saas-attendance-group-settings__radio-card--selected" : ""].join(" ") }, /* @__PURE__ */ react.default.createElement(antd.Radio, { value: "custom" }), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__radio-content" }, /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__radio-title" }, _pisell_utils.locales.getText("pisell2.attendance.group-settings.correction-policy.reset-period.custom-period.title")), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__radio-desc" }, _pisell_utils.locales.getText("pisell2.attendance.group-settings.correction-policy.reset-period.custom-period.desc"))))));
};
const CorrectionPolicyTab = (props) => {
	var _Form$useWatch, _Form$useWatch2;
	const { form } = props;
	const { members } = (0, react.useContext)(require_index.settingsContext);
	(0, 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, react.useMemo)(() => {
		if (!Array.isArray(members)) return [];
		return members.map((item) => ({
			label: item.nickname,
			value: item.id
		}));
	}, [members]);
	const customPeriodOptions = (0, react.useMemo)(() => Array.from({ length: 28 }, (_, index) => {
		const day = index + 1;
		return {
			label: String(day),
			value: day
		};
	}), []);
	const allow = (_Form$useWatch = antd.Form.useWatch(["correction", "allow"], form)) !== null && _Form$useWatch !== void 0 ? _Form$useWatch : true;
	const period_type = (_Form$useWatch2 = antd.Form.useWatch(["correction", "period_type"], form)) !== null && _Form$useWatch2 !== void 0 ? _Form$useWatch2 : "natural_month";
	(0, react.useEffect)(() => {
		var _form$getFieldValue;
		const correction = (_form$getFieldValue = form.getFieldValue(["correction"])) !== null && _form$getFieldValue !== void 0 ? _form$getFieldValue : {};
		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") {
			var _ref, _correction$period_ty;
			nextCorrection.period_custom = ((_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") === "custom" ? 1 : null;
		}
		if (Object.keys(nextCorrection).length > 0) form.setFieldsValue({ correction: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, correction), nextCorrection) });
	}, [form]);
	(0, 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__ */ react.default.createElement("div", {
		className: "saas-attendance-group-settings__panel",
		id: props.id
	}, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: ["correction", "allow"],
		valuePropName: "checked",
		initialValue: true
	}, /* @__PURE__ */ react.default.createElement(antd.Checkbox, {
		className: "saas-attendance-group-settings__allow-corrections",
		style: { width: "100%" }
	}, /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__allow-title" }, _pisell_utils.locales.getText("pisell2.attendance.group-settings.correction-policy.allow-corrections.title")), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__allow-desc" }, _pisell_utils.locales.getText("pisell2.attendance.group-settings.correction-policy.allow-corrections.desc")))), allow && /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__section" }, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: ["correction", "types"],
		initialValue: ["missing", "late"],
		label: _pisell_utils.locales.getText("pisell2.attendance.group-settings.correction-policy.types.label"),
		rules: [{
			required: true,
			message: _pisell_utils.locales.getText("pisell2.attendance.group-settings.correction-policy.types.label")
		}]
	}, /* @__PURE__ */ react.default.createElement(CorrectionTypesField, null))), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__section" }, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: ["correction", "approver_ids"],
		initialValue: [],
		label: _pisell_utils.locales.getText("pisell2.attendance.group-settings.correction-policy.approver.label"),
		rules: [{
			required: true,
			message: _pisell_utils.locales.getText("pisell2.attendance.group-settings.correction-policy.approver.required")
		}]
	}, /* @__PURE__ */ react.default.createElement(antd.Select, {
		allowClear: true,
		mode: "multiple",
		options: approverOptions,
		placeholder: _pisell_utils.locales.getText("pisell2.attendance.group-settings.correction-policy.approver.placeholder"),
		showSearch: true,
		optionFilterProp: "label"
	}))), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__section" }, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: ["correction", "days_limit"],
		initialValue: 7,
		label: _pisell_utils.locales.getText("pisell2.attendance.group-settings.correction-policy.time-limit.label"),
		rules: [{
			required: true,
			message: _pisell_utils.locales.getText("pisell2.attendance.group-settings.correction-policy.time-limit.required")
		}]
	}, /* @__PURE__ */ react.default.createElement(CorrectionTimeLimitField, null))), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__section" }, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: ["correction", "monthly_times"],
		initialValue: 3,
		label: _pisell_utils.locales.getText("pisell2.attendance.group-settings.correction-policy.monthly-limit.label"),
		rules: [{
			required: true,
			message: _pisell_utils.locales.getText("pisell2.attendance.group-settings.correction-policy.monthly-limit.required")
		}]
	}, /* @__PURE__ */ react.default.createElement(CorrectionMonthlyLimitField, null))), /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__section" }, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: ["correction", "period_type"],
		initialValue: "natural_month",
		label: _pisell_utils.locales.getText("pisell2.attendance.group-settings.correction-policy.reset-period.label")
	}, /* @__PURE__ */ react.default.createElement(CorrectionResetPeriodField, null))), period_type === "custom" && /* @__PURE__ */ react.default.createElement("div", { className: "saas-attendance-group-settings__section" }, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: ["correction", "period_custom"],
		initialValue: 1,
		rules: [{
			required: true,
			message: _pisell_utils.locales.getText("pisell2.attendance.group-settings.correction-policy.reset-period.custom-period.required")
		}]
	}, /* @__PURE__ */ react.default.createElement(antd.Select, { options: customPeriodOptions })))));
};
//#endregion
exports.default = CorrectionPolicyTab;
