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/components/pisellSelectCustomerModal/RightPanel/Add/BirthdayField/index.tsx
var BirthdayField_exports = {};
__export(BirthdayField_exports, {
  default: () => BirthdayField_default
});
module.exports = __toCommonJS(BirthdayField_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_debounce = __toESM(require("lodash/debounce"));
var import_dayjs = __toESM(require("dayjs"));
var import_utils = require("@pisell/utils");
var { Option } = import_antd.Select;
var getSelectInput = (id) => {
  const input = document.getElementById(id);
  if (input) {
    return input;
  }
};
var setSelectInputType = ({
  input,
  type
}) => {
  if (input) {
    input.type = type;
  }
};
var DateSectionCom = ({
  value,
  onChange,
  showTime,
  className = "",
  style = {},
  useSpace = true,
  format,
  itemStyle = {
    width: 100
  }
}) => {
  const currentYear = (0, import_dayjs.default)().year();
  const locale = import_utils.locales.locale;
  const dayRef = (0, import_react.useRef)(null);
  const monthRef = (0, import_react.useRef)(null);
  const yearRef = (0, import_react.useRef)(null);
  const [date, setDate] = (0, import_react.useState)({
    year: value ? (0, import_dayjs.default)(value).year() : null,
    month: value ? (0, import_dayjs.default)(value).month() + 1 : null,
    day: value ? (0, import_dayjs.default)(value).date() : null
  });
  const [dayOptions, setDayOptions] = (0, import_react.useState)([]);
  const [monthOptions, setMonthOptions] = (0, import_react.useState)([]);
  const [yearOptions, setYearOptions] = (0, import_react.useState)([]);
  const localeMap = {
    year: import_utils.locales.getText("pisell2.text.date-year-text"),
    month: import_utils.locales.getText("pisell2.text.date-month-text"),
    day: import_utils.locales.getText("pisell2.text.date-day-text")
  };
  (0, import_react.useEffect)(() => {
    setDate({
      year: value ? (0, import_dayjs.default)(value).year() : null,
      month: value ? (0, import_dayjs.default)(value).month() + 1 : null,
      day: value ? (0, import_dayjs.default)(value).date() : null
    });
  }, [value]);
  (0, import_react.useEffect)(() => {
    setMonthOptions(renderOptions("month", 1, 12));
    setYearOptions(renderOptions("year", currentYear - 100, currentYear + 50));
  }, []);
  (0, import_react.useEffect)(() => {
    setDayOptions(
      renderOptions(
        "day",
        1,
        date.month ? getDaysInMonth(date.year || currentYear, date.month) : 31
      )
    );
  }, [date.month, date.year]);
  const handleDateChange = (field, value2) => {
    if (!value2) {
      return;
    }
    let newDate = {
      year: date.year,
      month: date.month,
      day: date.day,
      [field]: value2
    };
    const foucsList = {
      day: monthRef,
      month: yearRef
    };
    if (field === "month" || field === "year") {
      const daysInMonth = getDaysInMonth(
        (newDate == null ? void 0 : newDate.year) || currentYear,
        newDate == null ? void 0 : newDate.month
      );
      const newDay = (newDate == null ? void 0 : newDate.day) > daysInMonth ? daysInMonth : newDate.day;
      newDate = {
        ...newDate,
        day: newDay
      };
    }
    setDate(newDate);
    if (foucsList[field]) {
      foucsList[field].current.focus();
    } else {
      yearRef.current.blur();
    }
    const _format = format || "YYYY-MM-DD";
    if (newDate.year && newDate.month && newDate.day) {
      const _date = `${newDate.year}-${newDate.month.toString().padStart(2, "0")}-${newDate.day.toString().padStart(2, "0")}`;
      onChange((0, import_dayjs.default)(_date).format(_format));
    }
  };
  const getDaysInMonth = (year, month) => {
    return (0, import_dayjs.default)(`${year}-${month}`, "YYYY-MM").daysInMonth();
  };
  const renderOptions = (field, start, end) => {
    const options = [];
    const _text = locale !== "en-US" && locale !== "en" ? localeMap[field] : "";
    for (let i = start; i <= end; i++) {
      options.push({
        label: `${i}${_text}`,
        value: i
      });
    }
    return options;
  };
  const selectId = (0, import_react.useMemo)(() => {
    return {
      dayId: (0, import_utils.getUniqueId)("day"),
      monthId: (0, import_utils.getUniqueId)("month"),
      yearId: (0, import_utils.getUniqueId)("year")
    };
  }, []);
  (0, import_react.useLayoutEffect)(() => {
    setSelectInputType({
      input: getSelectInput(selectId.dayId),
      type: "number"
    });
    setSelectInputType({
      input: getSelectInput(selectId.monthId),
      type: "number"
    });
    setSelectInputType({
      input: getSelectInput(selectId.yearId),
      type: "number"
    });
  }, [selectId]);
  const handleDateSearch = (0, import_react.useCallback)(
    (0, import_debounce.default)((field, value2) => {
      const dateOption = {
        day: dayOptions,
        month: monthOptions,
        year: yearOptions
      };
      const _value = Number(value2);
      const index = dateOption[field].findIndex((item) => {
        return item.value === _value;
      });
      if (index !== -1) {
        handleDateChange(field, _value);
      } else {
        handleDateChange(field, null);
      }
    }, 300),
    [dayOptions, monthOptions, yearOptions, date]
  );
  const content = /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Select,
    {
      id: selectId.dayId,
      value: date.day,
      placeholder: import_utils.locales.getText("pisell2.text.date-day-text"),
      onChange: (value2) => handleDateChange("day", value2),
      style: itemStyle,
      showSearch: true,
      onSearch: (value2) => {
        handleDateSearch("day", value2);
      },
      ref: dayRef,
      size: "large"
    },
    dayOptions.map((item) => {
      return /* @__PURE__ */ import_react.default.createElement(Option, { key: item.value, value: item.value }, item.label);
    })
  ), /* @__PURE__ */ import_react.default.createElement(
    import_antd.Select,
    {
      id: selectId.monthId,
      value: date.month,
      placeholder: import_utils.locales.getText("pisell2.text.date-month-text"),
      onChange: (value2) => handleDateChange("month", value2),
      style: itemStyle,
      showSearch: true,
      onSearch: (value2) => {
        handleDateSearch("month", value2);
      },
      ref: monthRef,
      size: "large"
    },
    monthOptions.map((item) => {
      return /* @__PURE__ */ import_react.default.createElement(Option, { key: item.value, value: item.value }, item.label);
    })
  ), /* @__PURE__ */ import_react.default.createElement(
    import_antd.Select,
    {
      id: selectId.yearId,
      value: date.year,
      placeholder: import_utils.locales.getText("pisell2.text.date-year-text"),
      onChange: (value2) => handleDateChange("year", value2),
      style: itemStyle,
      showSearch: true,
      onSearch: (value2) => {
        handleDateSearch("year", value2);
      },
      ref: yearRef,
      size: "large"
    },
    yearOptions.map((item) => {
      return /* @__PURE__ */ import_react.default.createElement(Option, { key: item.value, value: item.value }, item.label);
    })
  ));
  if (useSpace) {
    return /* @__PURE__ */ import_react.default.createElement(import_antd.Space, { className: className || "", style: style || {} }, content);
  }
  return /* @__PURE__ */ import_react.default.createElement("div", { className: className || "", style: style || {} }, content);
};
var BirthdayField_default = DateSectionCom;
