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/pisellMobileDateRangePicker/PisellMobileDateRangePicker.tsx
var PisellMobileDateRangePicker_exports = {};
__export(PisellMobileDateRangePicker_exports, {
  PisellMobileDateRangePicker: () => PisellMobileDateRangePicker
});
module.exports = __toCommonJS(PisellMobileDateRangePicker_exports);
var import_react = __toESM(require("react"));
var import_antd_mobile = require("antd-mobile");
var import_dayjs = __toESM(require("dayjs"));
var import_classnames = __toESM(require("classnames"));
var import_Calendar = __toESM(require("@pisell/icon/es/Calendar"));
var import_zh_CN = __toESM(require("antd-mobile/es/locales/zh-CN"));
var import_zh_TW = __toESM(require("antd-mobile/es/locales/zh-TW"));
var import_zh_HK = __toESM(require("antd-mobile/es/locales/zh-HK"));
var import_en_US = __toESM(require("antd-mobile/es/locales/en-US"));
var import_ja_JP = __toESM(require("antd-mobile/es/locales/ja-JP"));
var import_pt_BR = __toESM(require("antd-mobile/es/locales/pt-BR"));
var import_locales = require("../../locales");
var import_locale = require("../../utils/locale");
var import_types = require("./types");
var import_global = require("antd-mobile/es/global/global.css");
var import_PisellMobileDateRangePicker = require("./PisellMobileDateRangePicker.less");
var ANTD_MOBILE_LOCALE_MAP = {
  "zh-CN": import_zh_CN.default,
  "zh-TW": import_zh_TW.default,
  "zh-HK": import_zh_HK.default,
  "en-US": import_en_US.default,
  en: import_en_US.default,
  original: import_en_US.default,
  ja: import_ja_JP.default,
  pt: import_pt_BR.default
};
var FILTER_POPUP_Z_INDEX = 1100;
function getDefaultMin() {
  return (0, import_dayjs.default)().subtract(1, "year").startOf("month").toDate();
}
function getDefaultMax() {
  return (0, import_dayjs.default)().endOf("month").toDate();
}
function toDayjsRange(val) {
  if (val == null) return null;
  if (!Array.isArray(val) || val.length < 2) return null;
  const start = val[0] ? import_dayjs.default.isDayjs(val[0]) ? val[0] : (0, import_dayjs.default)(val[0]) : null;
  const end = val[1] ? import_dayjs.default.isDayjs(val[1]) ? val[1] : (0, import_dayjs.default)(val[1]) : null;
  if (!(start == null ? void 0 : start.isValid()) || !(end == null ? void 0 : end.isValid())) return null;
  return [start, end];
}
function toDateRange(val) {
  const normalized = toDayjsRange(val);
  if (!normalized) return null;
  return [normalized[0].toDate(), normalized[1].toDate()];
}
function formatRangeLabel(range, format, placeholder) {
  if (!range) return placeholder;
  return `${range[0].format(format)} - ${range[1].format(format)}`;
}
function resolveFilterGetContainer() {
  if (typeof document === "undefined") return null;
  return document.body;
}
var PisellMobileDateRangePicker = ({
  value,
  onChange,
  format = "DD/MM/YYYY",
  placeholder,
  className,
  style,
  title,
  confirmText,
  allowClear = true,
  min,
  max,
  variant = "toolbar",
  getContainer,
  popupClassName,
  popupStyle,
  onOpen,
  onClose
}) => {
  const [visible, setVisible] = (0, import_react.useState)(false);
  const dayjsRange = (0, import_react.useMemo)(() => toDayjsRange(value), [value]);
  const pickerValue = (0, import_react.useMemo)(() => toDateRange(value), [value]);
  const [innerValue, setInnerValue] = (0, import_react.useState)(
    pickerValue
  );
  (0, import_react.useEffect)(() => {
    if (visible) {
      setInnerValue(pickerValue);
    }
  }, [visible]);
  const resolvedPlaceholder = placeholder ?? (0, import_locales.getText)("pisell-mobile-date-range-picker-placeholder");
  const resolvedTitle = title ?? (0, import_locales.getText)("pisell-mobile-date-range-picker-title");
  const resolvedConfirmText = confirmText ?? (0, import_locales.getText)("pisell-mobile-date-range-picker-confirm");
  const antMobileLocale = ANTD_MOBILE_LOCALE_MAP[(0, import_locale.getCurrentLocale)()] ?? import_en_US.default;
  const isFilterVariant = variant === "filter";
  const openPicker = (0, import_react.useCallback)(() => {
    setVisible(true);
    onOpen == null ? void 0 : onOpen();
  }, [onOpen]);
  const closePicker = (0, import_react.useCallback)(() => {
    setVisible(false);
    onClose == null ? void 0 : onClose();
  }, [onClose]);
  const handleConfirm = (0, import_react.useCallback)(
    (next) => {
      if (!next) {
        onChange == null ? void 0 : onChange(void 0);
        closePicker();
        return;
      }
      onChange == null ? void 0 : onChange([
        (0, import_dayjs.default)(next[0]).startOf("day"),
        (0, import_dayjs.default)(next[1]).startOf("day")
      ]);
      closePicker();
    },
    [onChange, closePicker]
  );
  const resolvedGetContainer = getContainer ?? (isFilterVariant ? resolveFilterGetContainer : void 0);
  const resolvedPopupClassName = (0, import_classnames.default)(
    isFilterVariant ? import_types.PISELL_MOBILE_DATE_RANGE_PICKER_FILTER_POPUP_CLASS : null,
    popupClassName
  );
  const resolvedPopupStyle = isFilterVariant ? { ...popupStyle, zIndex: (popupStyle == null ? void 0 : popupStyle.zIndex) ?? FILTER_POPUP_Z_INDEX } : popupStyle;
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)(
        "pisell-mobile-date-range-picker",
        {
          "pisell-mobile-date-range-picker--filter": isFilterVariant
        },
        className
      ),
      style
    },
    /* @__PURE__ */ import_react.default.createElement(
      "button",
      {
        type: "button",
        className: (0, import_classnames.default)("pisell-mobile-date-range-picker__trigger", {
          "pisell-mobile-date-range-picker__trigger--placeholder": !dayjsRange
        }),
        onClick: openPicker
      },
      /* @__PURE__ */ import_react.default.createElement("span", { className: "pisell-mobile-date-range-picker__trigger-text" }, formatRangeLabel(dayjsRange, format, resolvedPlaceholder)),
      isFilterVariant ? /* @__PURE__ */ import_react.default.createElement(
        import_Calendar.default,
        {
          className: "pisell-mobile-date-range-picker__trigger-icon",
          "aria-hidden": true
        }
      ) : null
    ),
    /* @__PURE__ */ import_react.default.createElement(import_antd_mobile.ConfigProvider, { locale: antMobileLocale }, /* @__PURE__ */ import_react.default.createElement(
      import_antd_mobile.CalendarPicker,
      {
        visible,
        selectionMode: "range",
        value: innerValue ?? void 0,
        onChange: (v) => setInnerValue(v),
        onClose: closePicker,
        onMaskClick: closePicker,
        closeOnMaskClick: true,
        onConfirm: handleConfirm,
        title: resolvedTitle,
        confirmText: resolvedConfirmText,
        allowClear,
        min: min ?? getDefaultMin(),
        max: max ?? getDefaultMax(),
        getContainer: resolvedGetContainer,
        popupClassName: resolvedPopupClassName || void 0,
        popupStyle: resolvedPopupStyle
      }
    ))
  );
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  PisellMobileDateRangePicker
});
