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/pro/multiDayTimeSelect/MultiDayTimeSelect.tsx
var MultiDayTimeSelect_exports = {};
__export(MultiDayTimeSelect_exports, {
  default: () => MultiDayTimeSelect_default
});
module.exports = __toCommonJS(MultiDayTimeSelect_exports);
var import_react = __toESM(require("react"));
var import_icons = require("@ant-design/icons");
var import_dayjs = __toESM(require("dayjs"));
var import_materials = require("@pisell/materials");
var import_antd = require("antd");
var import_utils = require("@pisell/utils");
var import_dateRange = require("../../components/booking/info/dateRange");
var import_MultiDayTimeSelect = require("./MultiDayTimeSelect.less");
var RangePicker = import_materials.DatePicker.RangePicker;
var MultiDayTimeSelect = (0, import_react.forwardRef)(({
  maxDays = 30,
  defaultStartDate,
  separateStartEnd = false,
  value,
  onChange,
  locale = "en",
  disabled = false,
  startDateLabel,
  endDateLabel,
  durationLabel
}, ref) => {
  var _a, _b, _c, _d;
  const [form] = import_antd.Form.useForm();
  const [internalValue, setInternalValue] = (0, import_react.useState)(null);
  (0, import_react.useImperativeHandle)(ref, () => ({
    validate: async () => {
      await form.validateFields();
    },
    getValue: () => internalValue
  }));
  const formatLocale = (0, import_react.useMemo)(() => {
    const isEn = locale === "en" || locale === "en-US";
    return isEn ? "DD/MM/YYYY" : "YYYY-MM-DD";
  }, [locale]);
  const durationOptions = (0, import_react.useMemo)(() => {
    const formatDurationLabel = import_utils.locales.getText("pisell2.text.multi-day-duration");
    const getDurationLabel = (days) => typeof formatDurationLabel === "function" ? formatDurationLabel(days) : `${days} day`;
    const options = [];
    for (let i = 1; i <= maxDays; i++) {
      options.push({
        label: getDurationLabel(i),
        value: i
      });
    }
    const currentDuration = internalValue == null ? void 0 : internalValue.duration;
    if (currentDuration && currentDuration > maxDays) {
      options.push({
        label: getDurationLabel(currentDuration),
        value: currentDuration,
        disabled: true
      });
    }
    return options;
  }, [maxDays, ((_b = (_a = import_utils.locales).getLocale) == null ? void 0 : _b.call(_a)) || locale, internalValue == null ? void 0 : internalValue.duration]);
  (0, import_react.useEffect)(() => {
    if (value) {
      setInternalValue(value);
      form.setFieldsValue({
        dateRange: separateStartEnd ? void 0 : [value.startDate, value.endDate],
        startDate: separateStartEnd ? value.startDate : void 0,
        endDate: separateStartEnd ? value.endDate : void 0,
        duration: value.duration
      });
    } else {
      const startDate = defaultStartDate || (0, import_dayjs.default)();
      const endDate = startDate.add(1, "day");
      const initialValue = {
        startDate,
        endDate,
        duration: 1
      };
      setInternalValue(initialValue);
      form.setFieldsValue({
        dateRange: separateStartEnd ? void 0 : [startDate, endDate],
        startDate: separateStartEnd ? startDate : void 0,
        endDate: separateStartEnd ? endDate : void 0,
        duration: 1
      });
      onChange == null ? void 0 : onChange(initialValue);
    }
  }, [value, defaultStartDate, separateStartEnd, form, onChange]);
  const updateValue = (newValue) => {
    setInternalValue(newValue);
    onChange == null ? void 0 : onChange(newValue);
  };
  const handleDateRangeChange = (dates) => {
    if (dates && dates[0] && dates[1]) {
      const days = dates[1].diff(dates[0], "day");
      let finalEndDate = dates[1];
      let finalDuration = days;
      if (days > 0) {
        const newValue = {
          startDate: dates[0],
          endDate: finalEndDate,
          duration: finalDuration
        };
        updateValue(newValue);
        form.setFieldsValue({
          dateRange: [dates[0], finalEndDate],
          duration: finalDuration
        });
      }
    }
  };
  const handleStartDateChange = (date) => {
    if (date && internalValue) {
      const currentEndDate = internalValue.endDate;
      const endTime = {
        hour: currentEndDate.hour(),
        minute: currentEndDate.minute(),
        second: currentEndDate.second(),
        millisecond: currentEndDate.millisecond()
      };
      const targetDuration = Math.min(internalValue.duration, maxDays);
      let newEndDate = date.add(targetDuration, "day");
      const finalEndDate = newEndDate.hour(endTime.hour).minute(endTime.minute).second(endTime.second).millisecond(endTime.millisecond);
      const newValue = {
        startDate: date,
        endDate: finalEndDate,
        duration: targetDuration
      };
      updateValue(newValue);
      form.setFieldsValue({
        endDate: finalEndDate,
        duration: targetDuration
      });
    }
  };
  const handleEndDateChange = (date) => {
    if (date && internalValue) {
      const days = date.diff(internalValue.startDate, "day");
      if (days > 0) {
        const newValue = {
          startDate: internalValue.startDate,
          endDate: date,
          duration: days
        };
        updateValue(newValue);
        form.setFieldsValue({
          endDate: date,
          duration: days
        });
      }
    }
  };
  const handleDurationChange = (durationValue) => {
    if (internalValue) {
      const newEndDate = internalValue.startDate.add(durationValue, "day");
      const newValue = {
        startDate: internalValue.startDate,
        endDate: newEndDate,
        duration: durationValue
      };
      updateValue(newValue);
      if (separateStartEnd) {
        form.setFieldsValue({ endDate: newEndDate });
      } else {
        form.setFieldsValue({ dateRange: [internalValue.startDate, newEndDate] });
      }
    }
  };
  return /* @__PURE__ */ import_react.default.createElement(import_antd.Form, { form, layout: "vertical", className: "multi-day-time-select-form" }, separateStartEnd ? /* @__PURE__ */ import_react.default.createElement("div", { className: "multi-day-time-select-row" }, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Form.Item,
    {
      label: startDateLabel || import_utils.locales.getText("pisell2.text.start_date"),
      name: "startDate",
      rules: [{ required: true, message: startDateLabel || import_utils.locales.getText("pisell2.text.start_date") }],
      style: { flex: 1 }
    },
    /* @__PURE__ */ import_react.default.createElement(
      import_materials.DatePicker,
      {
        inputReadOnly: true,
        allowClear: false,
        size: "large",
        style: { width: "100%", padding: 0 },
        suffixIcon: /* @__PURE__ */ import_react.default.createElement(import_icons.CalendarOutlined, null),
        value: internalValue == null ? void 0 : internalValue.startDate,
        onChange: handleStartDateChange,
        format: formatLocale,
        isHideCustomSelect: true,
        disabled
      }
    )
  ), /* @__PURE__ */ import_react.default.createElement(
    import_antd.Form.Item,
    {
      label: endDateLabel || import_utils.locales.getText("pisell2.text.end_date"),
      name: "endDate",
      rules: [{ required: true, message: endDateLabel || import_utils.locales.getText("pisell2.text.end_date") }],
      style: { flex: 1 }
    },
    /* @__PURE__ */ import_react.default.createElement(
      import_materials.DatePicker,
      {
        inputReadOnly: true,
        allowClear: false,
        size: "large",
        style: { width: "100%", padding: 0 },
        suffixIcon: /* @__PURE__ */ import_react.default.createElement(import_icons.CalendarOutlined, null),
        value: internalValue == null ? void 0 : internalValue.endDate,
        onChange: handleEndDateChange,
        format: formatLocale,
        isHideCustomSelect: true,
        disabled,
        disabledDate: (current) => {
          if (internalValue == null ? void 0 : internalValue.startDate) {
            return current.isSameOrBefore(internalValue.startDate, "day");
          }
          return false;
        }
      }
    )
  )) : /* @__PURE__ */ import_react.default.createElement("div", { className: "multi-day-time-select-row" }, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Form.Item,
    {
      label: startDateLabel || import_utils.locales.getText("pisell2.text.start_date"),
      name: "dateRange",
      rules: [{ required: true, message: startDateLabel || import_utils.locales.getText("pisell2.text.start_date") }],
      style: { flex: 1 }
    },
    /* @__PURE__ */ import_react.default.createElement(
      import_materials.Page,
      {
        isRenderPisellConfigProvider: false,
        locale: ((_d = (_c = import_utils.locales).getLocale) == null ? void 0 : _d.call(_c)) || locale
      },
      /* @__PURE__ */ import_react.default.createElement(
        RangePicker,
        {
          inputReadOnly: true,
          allowClear: false,
          size: "large",
          style: { width: "100%", padding: 0 },
          suffixIcon: /* @__PURE__ */ import_react.default.createElement(import_icons.CalendarOutlined, null),
          value: internalValue ? [internalValue.startDate, internalValue.endDate] : null,
          onChange: handleDateRangeChange,
          format: formatLocale,
          isHideCustomSelect: true,
          endDateDays: 1,
          disabled,
          disabledDate: import_dateRange.disabledDate
        }
      )
    )
  )), /* @__PURE__ */ import_react.default.createElement(
    import_antd.Form.Item,
    {
      label: durationLabel || import_utils.locales.getText("pisell2.text.duration"),
      name: "duration",
      rules: [{ required: true, message: durationLabel || import_utils.locales.getText("pisell2.text.duration") }]
    },
    /* @__PURE__ */ import_react.default.createElement(
      import_antd.Select,
      {
        size: "large",
        value: internalValue == null ? void 0 : internalValue.duration,
        onChange: handleDurationChange,
        options: durationOptions,
        disabled
      }
    )
  ));
});
MultiDayTimeSelect.displayName = "MultiDayTimeSelect";
var MultiDayTimeSelect_default = MultiDayTimeSelect;
