import useEngineContext from "../../../../hooks/useEngineContext.js";
import locales_default from "../../locales.js";
import dayjs from "dayjs";
import React, { useEffect, useMemo, useState } from "react";
import { DatePicker, Form, Select } from "antd";
import { locales } from "@pisell/utils";
//#region src/plus/walletEditor/components/ValidityPeriodSelector/index.tsx
const { Option } = Select;
const DEFAULT_VALIDITY_OPTIONS = [
	{
		unit: "1",
		unit_type: "day",
		type: "normal"
	},
	{
		unit: "14",
		unit_type: "day",
		type: "normal"
	},
	{
		unit: "1",
		unit_type: "month",
		type: "normal"
	},
	{
		unit: "2",
		unit_type: "month",
		type: "normal"
	},
	{
		unit: "3",
		unit_type: "month",
		type: "normal"
	},
	{
		unit: "4",
		unit_type: "month",
		type: "normal"
	},
	{
		unit: "5",
		unit_type: "month",
		type: "normal"
	},
	{
		unit: "6",
		unit_type: "month",
		type: "normal"
	},
	{
		unit: "1",
		unit_type: "year",
		type: "normal"
	},
	{
		unit: "3",
		unit_type: "year",
		type: "normal"
	},
	{ type: "forever" },
	{
		custom_date: "",
		type: "custom"
	}
];
const getOptionLabel = (option) => {
	if (option.type === "forever") return locales.getText("forever");
	if (option.type === "custom") return locales.getText("custom");
	const unitMap = {
		day: locales.getText("day"),
		month: locales.getText("month"),
		year: locales.getText("year"),
		days: locales.getText("days"),
		months: locales.getText("months"),
		years: locales.getText("years")
	};
	const { unit, unit_type } = option;
	if (Number(unit) > 1) return `${unit} ${unitMap[unit_type + "s"]}`;
	return `${unit} ${unitMap[unit_type]}`;
};
const transformValueToString = (option) => {
	if (option.type === "forever") return "forever";
	if (option.type === "custom") return "custom";
	return `${option.unit}${option.unit_type}`;
};
const transformStringToValue = (stringValue) => {
	if (stringValue === "forever") return { type: "forever" };
	if (stringValue === "custom") return {
		type: "custom",
		custom_date: ""
	};
	const match = stringValue.match(/^(\d+)(day|month|year)$/);
	if (match) {
		const [, unit, unit_type] = match;
		return {
			type: "normal",
			unit,
			unit_type
		};
	}
	return {
		type: "normal",
		unit: "1",
		unit_type: "day"
	};
};
const ValidityPeriodSelector = ({ value, onChange, disabled = false, placeholder, options = DEFAULT_VALIDITY_OPTIONS }) => {
	var _context$appHelper$ut;
	const context = useEngineContext();
	locales.init(locales_default, ((_context$appHelper$ut = context.appHelper.utils) === null || _context$appHelper$ut === void 0 || (_context$appHelper$ut = _context$appHelper$ut.storage) === null || _context$appHelper$ut === void 0 ? void 0 : _context$appHelper$ut.get("umi_locale")) || "en");
	const defaultPlaceholder = placeholder || locales.getText("selectValidityPeriod");
	const [showCustomDate, setShowCustomDate] = useState(false);
	const [currentValue, setCurrentValue] = useState(value);
	const validityOptions = useMemo(() => {
		return options.map((option) => ({
			label: getOptionLabel(option),
			value: transformValueToString(option)
		}));
	}, [options]);
	useEffect(() => {
		setCurrentValue(value);
		setShowCustomDate((value === null || value === void 0 ? void 0 : value.type) === "custom");
	}, [value]);
	const handleSelectChange = (selectedValue) => {
		const parsedValue = transformStringToValue(selectedValue);
		setCurrentValue(parsedValue);
		setShowCustomDate(parsedValue.type === "custom");
		if (parsedValue.type !== "custom") onChange === null || onChange === void 0 || onChange(parsedValue);
	};
	const handleDateChange = (date, dateString) => {
		if (date && (currentValue === null || currentValue === void 0 ? void 0 : currentValue.type) === "custom") {
			const newValue = {
				type: "custom",
				custom_date: date.format("YYYY-MM-DD")
			};
			setCurrentValue(newValue);
			onChange === null || onChange === void 0 || onChange(newValue);
		}
	};
	const getSelectValue = () => {
		if (!currentValue) return void 0;
		return transformValueToString(currentValue);
	};
	const renderOptions = () => {
		return validityOptions.map((option) => /* @__PURE__ */ React.createElement(Option, {
			key: option.label,
			value: option.value
		}, option.label));
	};
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(Select, {
		placeholder: defaultPlaceholder,
		onChange: handleSelectChange,
		value: getSelectValue(),
		disabled,
		style: { width: "100%" }
	}, renderOptions()), showCustomDate && /* @__PURE__ */ React.createElement(Form.Item, {
		label: locales.getText("custom"),
		style: { marginBottom: 0 }
	}, /* @__PURE__ */ React.createElement(DatePicker, {
		placeholder: locales.getText("selectCustomDate"),
		onChange: handleDateChange,
		value: (currentValue === null || currentValue === void 0 ? void 0 : currentValue.type) === "custom" && currentValue.custom_date ? dayjs(currentValue.custom_date) : void 0,
		disabled,
		style: { width: "100%" }
	})));
};
//#endregion
export { ValidityPeriodSelector as default };
