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/pisellReservationSchedule/PisellReservationSchedule.tsx
var PisellReservationSchedule_exports = {};
__export(PisellReservationSchedule_exports, {
  PisellReservationSchedule: () => PisellReservationSchedule
});
module.exports = __toCommonJS(PisellReservationSchedule_exports);
var import_react = __toESM(require("react"));
var import_dayjs = __toESM(require("dayjs"));
var import_classnames = __toESM(require("classnames"));
var import_antd = require("antd");
var import_date_picker = require("@pisell/date-picker");
var import_ChevronLeft = __toESM(require("@pisell/icon/es/ChevronLeft"));
var import_ChevronRight = __toESM(require("@pisell/icon/es/ChevronRight"));
var import_utils = require("@pisell/utils");
var import_useEngineContext = __toESM(require("../../hooks/useEngineContext"));
var import_locales = __toESM(require("./locales"));
var import_PisellReservationSchedule = require("./PisellReservationSchedule.less");
var I18N_TODAY = "pisell-reservation-schedule.today";
var I18N_PREV = "pisell-reservation-schedule.prev";
var I18N_NEXT = "pisell-reservation-schedule.next";
var I18N_PREV_WEEK = "pisell-reservation-schedule.prev-week";
var I18N_NEXT_WEEK = "pisell-reservation-schedule.next-week";
var I18N_PREV_MONTH = "pisell-reservation-schedule.prev-month";
var I18N_NEXT_MONTH = "pisell-reservation-schedule.next-month";
var I18N_PICK_DATE = "pisell-reservation-schedule.pick-date";
var I18N_PICK_DATE_TITLE = "pisell-reservation-schedule.pick-date-title";
var I18N_PICK_WEEK_TITLE = "pisell-reservation-schedule.pick-week-title";
var I18N_PICK_MONTH_TITLE = "pisell-reservation-schedule.pick-month-title";
function parseDay(input, fallback) {
  if (input == null) return fallback.startOf("day");
  const d = (0, import_dayjs.default)(input);
  return (d.isValid() ? d : fallback).startOf("day");
}
function toDayjsLocale(engineLocale) {
  if (!engineLocale || engineLocale === "en") return "en";
  if (engineLocale === "zh-HK") return "zh-hk";
  return "zh-cn";
}
function startOfWeekMonday(d) {
  const day = d.day();
  const diff = day === 0 ? -6 : 1 - day;
  return d.add(diff, "day").startOf("day");
}
function PisellReservationSchedule(props) {
  var _a, _b;
  const {
    value: valueProp,
    onChange,
    defaultValue,
    minDate: minDateProp,
    maxDate: maxDateProp,
    navigationLocked = false,
    navigationLockedDisplay = "full",
    navigationUnit = "day",
    className,
    style
  } = props;
  const context = (0, import_useEngineContext.default)();
  const engineLocale = ((_b = (_a = context == null ? void 0 : context.engine) == null ? void 0 : _a.props) == null ? void 0 : _b.locale) || "en";
  import_utils.locales.init(import_locales.default, engineLocale);
  const today = (0, import_react.useMemo)(() => (0, import_dayjs.default)().startOf("day"), []);
  const isControlled = valueProp !== void 0;
  const [internal, setInternal] = (0, import_react.useState)(
    () => parseDay(valueProp ?? defaultValue, today)
  );
  const value = isControlled ? parseDay(valueProp, today) : internal;
  const minDate = (0, import_react.useMemo)(
    () => minDateProp != null ? parseDay(minDateProp, today) : void 0,
    [minDateProp, today]
  );
  const maxDate = (0, import_react.useMemo)(
    () => maxDateProp != null ? parseDay(maxDateProp, today) : void 0,
    [maxDateProp, today]
  );
  const emit = (0, import_react.useCallback)(
    (next) => {
      let d = next.startOf("day");
      if (minDate && d.isBefore(minDate)) d = minDate;
      if (maxDate && d.isAfter(maxDate)) d = maxDate;
      if (!isControlled) setInternal(d);
      onChange == null ? void 0 : onChange(d);
    },
    [isControlled, onChange, minDate, maxDate]
  );
  const prevTarget = (0, import_react.useMemo)(() => {
    if (navigationUnit === "month") return value.subtract(1, "month");
    if (navigationUnit === "week") return value.subtract(7, "day");
    return value.subtract(1, "day");
  }, [navigationUnit, value]);
  const nextTarget = (0, import_react.useMemo)(() => {
    if (navigationUnit === "month") return value.add(1, "month");
    if (navigationUnit === "week") return value.add(7, "day");
    return value.add(1, "day");
  }, [navigationUnit, value]);
  const prevDisabled = navigationLocked || (minDate ? prevTarget.isBefore(minDate, "day") : false);
  const nextDisabled = navigationLocked || (maxDate ? nextTarget.isAfter(maxDate, "day") : false);
  const [calendarOpen, setCalendarOpen] = (0, import_react.useState)(false);
  const centerLabel = (0, import_react.useMemo)(() => {
    const djLoc = toDayjsLocale(engineLocale);
    const dl = value.locale(djLoc);
    if (navigationUnit === "month") {
      return dl.format(djLoc === "en" ? "MMM YYYY" : "YYYY年M月");
    }
    if (navigationUnit === "week") {
      const ws = startOfWeekMonday(value);
      const we = ws.add(6, "day");
      if (djLoc === "en") {
        return `${ws.format("MMM D")} – ${we.format("MMM D, YYYY")}`;
      }
      return `${ws.format("M月D日")} – ${we.format("M月D日")}`;
    }
    const isToday = value.isSame(today, "day");
    const todayText = import_utils.locales.getText(I18N_TODAY);
    const dateStr = djLoc === "en" ? dl.format("MMM D") : dl.format("M月D日");
    const weekStr = dl.format("ddd");
    const parts = [];
    if (isToday && todayText) parts.push(todayText);
    parts.push(dateStr);
    parts.push(weekStr);
    return parts.join(djLoc === "en" ? " · " : " · ");
  }, [value, today, engineLocale, navigationUnit]);
  const prevAria = navigationUnit === "month" ? import_utils.locales.getText(I18N_PREV_MONTH) : navigationUnit === "week" ? import_utils.locales.getText(I18N_PREV_WEEK) : import_utils.locales.getText(I18N_PREV);
  const nextAria = navigationUnit === "month" ? import_utils.locales.getText(I18N_NEXT_MONTH) : navigationUnit === "week" ? import_utils.locales.getText(I18N_NEXT_WEEK) : import_utils.locales.getText(I18N_NEXT);
  const pickAria = navigationUnit === "month" ? import_utils.locales.getText(I18N_PICK_MONTH_TITLE) : navigationUnit === "week" ? import_utils.locales.getText(I18N_PICK_WEEK_TITLE) : import_utils.locales.getText(I18N_PICK_DATE);
  const modalTitle = navigationUnit === "month" ? import_utils.locales.getText(I18N_PICK_MONTH_TITLE) : navigationUnit === "week" ? import_utils.locales.getText(I18N_PICK_WEEK_TITLE) : import_utils.locales.getText(I18N_PICK_DATE_TITLE);
  if (navigationLocked && navigationLockedDisplay === "labelOnly") {
    return /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: (0, import_classnames.default)(
          "pisell-reservation-schedule",
          "pisell-reservation-schedule--label-only",
          className
        ),
        style
      },
      /* @__PURE__ */ import_react.default.createElement(
        "span",
        {
          className: "pisell-reservation-schedule-label pisell-reservation-schedule-label--static",
          "aria-label": centerLabel
        },
        centerLabel
      )
    );
  }
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)("pisell-reservation-schedule", className),
      style
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-reservation-schedule-inner" }, /* @__PURE__ */ import_react.default.createElement(
      "button",
      {
        type: "button",
        className: "pisell-reservation-schedule-btn",
        "aria-label": prevAria,
        disabled: prevDisabled,
        onClick: () => {
          if (navigationLocked) return;
          emit(prevTarget);
        }
      },
      /* @__PURE__ */ import_react.default.createElement(import_ChevronLeft.default, { className: "pisell-reservation-schedule-btn-icon" })
    ), navigationLocked ? /* @__PURE__ */ import_react.default.createElement(
      "span",
      {
        className: "pisell-reservation-schedule-label pisell-reservation-schedule-label--static",
        "aria-label": centerLabel
      },
      centerLabel
    ) : /* @__PURE__ */ import_react.default.createElement(
      "button",
      {
        type: "button",
        className: "pisell-reservation-schedule-label pisell-reservation-schedule-label-trigger",
        "aria-label": pickAria,
        "aria-haspopup": "dialog",
        "aria-expanded": calendarOpen,
        onClick: () => setCalendarOpen(true)
      },
      centerLabel
    ), /* @__PURE__ */ import_react.default.createElement(
      "button",
      {
        type: "button",
        className: "pisell-reservation-schedule-btn",
        "aria-label": nextAria,
        disabled: nextDisabled,
        onClick: () => {
          if (navigationLocked) return;
          emit(nextTarget);
        }
      },
      /* @__PURE__ */ import_react.default.createElement(import_ChevronRight.default, { className: "pisell-reservation-schedule-btn-icon" })
    )),
    /* @__PURE__ */ import_react.default.createElement(
      import_antd.Modal,
      {
        title: modalTitle,
        open: !navigationLocked && calendarOpen,
        onCancel: () => setCalendarOpen(false),
        footer: null,
        width: "auto",
        centered: true,
        destroyOnClose: true,
        wrapClassName: "pisell-reservation-schedule-date-modal"
      },
      navigationUnit === "month" ? /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-reservation-schedule-month-picker-wrap" }, /* @__PURE__ */ import_react.default.createElement(
        import_date_picker.DateCalendar,
        {
          value,
          views: ["year", "month"],
          openTo: "month",
          minDate,
          maxDate,
          shouldDisableMonth: (month) => {
            const start = (0, import_dayjs.default)(month).startOf("month");
            if (minDate && start.isBefore(minDate.startOf("month"))) {
              return true;
            }
            if (maxDate && start.isAfter(maxDate.startOf("month"))) {
              return true;
            }
            return false;
          },
          onChange: (d) => {
            if (!d) return;
            emit((0, import_dayjs.default)(d).startOf("month"));
            setCalendarOpen(false);
          }
        }
      )) : /* @__PURE__ */ import_react.default.createElement(
        import_date_picker.DateCalendar,
        {
          value,
          minDate,
          maxDate,
          onChange: (d) => {
            if (!d) return;
            const picked = (0, import_dayjs.default)(d);
            if (navigationUnit === "week") {
              emit(startOfWeekMonday(picked));
            } else {
              emit(picked);
            }
            setCalendarOpen(false);
          }
        }
      )
    )
  );
}
PisellReservationSchedule.displayName = "PisellReservationSchedule";
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  PisellReservationSchedule
});
