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/eventBooking/components/scheduleCalendar/index.tsx
var scheduleCalendar_exports = {};
__export(scheduleCalendar_exports, {
  default: () => scheduleCalendar_default
});
module.exports = __toCommonJS(scheduleCalendar_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_dayjs = __toESM(require("dayjs"));
var import_calendarItem = __toESM(require("./calendarItem"));
var import_useEngineContext = __toESM(require("../../../../hooks/useEngineContext"));
var import_index = require("./index.less");
var EventScheduleCalendar = ({
  // 数据源
  data,
  showScheduleName = true,
  showScheduleTime = true,
  className,
  style,
  onChange,
  type = "calendarView"
}) => {
  const context = (0, import_useEngineContext.default)();
  const utils = context.appHelper.utils || {};
  const [selectedYear, setSelectedYear] = (0, import_react.useState)((0, import_dayjs.default)().year());
  const [currentMonth, setCurrentMonth] = (0, import_react.useState)(
    (/* @__PURE__ */ new Date()).toISOString().slice(0, 7)
  );
  const [selectedDate, setSelectedDate] = (0, import_react.useState)("");
  const [selectedTimes, setSelectedTimes] = (0, import_react.useState)([]);
  (0, import_react.useEffect)(() => {
    if (data == null ? void 0 : data.length) {
      const today = /* @__PURE__ */ new Date();
      today.setHours(0, 0, 0, 0);
      const firstAvailableDate = data.find((item) => {
        const itemDate = new Date(item.date);
        return itemDate >= today;
      });
      if (firstAvailableDate && (firstAvailableDate == null ? void 0 : firstAvailableDate.date)) {
        const availableDate = firstAvailableDate.date;
        const availableTimes = (firstAvailableDate == null ? void 0 : firstAvailableDate.times) || [];
        setSelectedYear((0, import_dayjs.default)(availableDate).year());
        setCurrentMonth(new Date(availableDate).toISOString().slice(0, 7));
        setSelectedDate(availableDate);
        setSelectedTimes(availableTimes || []);
      }
    }
  }, [data]);
  const handleSelectDate = (date, item) => {
    const dateData = data == null ? void 0 : data.find((d) => d.date === date);
    if (dateData) {
      setSelectedDate(date);
      setSelectedTimes(dateData.times || []);
    }
  };
  const formatDateStr = (time) => {
    var _a, _b;
    try {
      const locale = ((_a = utils == null ? void 0 : utils.storage) == null ? void 0 : _a.get("umi_locale")) || "en";
      const hour24 = (0, import_dayjs.default)(time).format("HH");
      const minutes = (0, import_dayjs.default)(time).format("mm");
      const isZh = (_b = locale.toLowerCase()) == null ? void 0 : _b.includes("zh");
      if (isZh) {
        const period = Number(hour24) < 12 ? "上午" : "下午";
        const hour12 = Number(hour24) % 12 || 12;
        return `${period} ${hour12}:${minutes}`;
      }
      return (0, import_dayjs.default)(time).format("hh:mm a").toLowerCase();
    } catch (error) {
    }
  };
  (0, import_react.useEffect)(() => {
    onChange == null ? void 0 : onChange(selectedYear);
  }, [selectedYear]);
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)("schedule-calendar-wrap", className),
      style
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: `month-item-box` }, /* @__PURE__ */ import_react.default.createElement(
      import_calendarItem.default,
      {
        isEdit: false,
        month: currentMonth,
        schedulesData: data || [],
        selectDate: selectedDate,
        onSelect: handleSelectDate,
        onChange: (e, type2) => {
          setCurrentMonth(e);
          const currentYear = (0, import_dayjs.default)().year();
          const selectedYear2 = (0, import_dayjs.default)(e).year();
          setSelectedYear(selectedYear2);
        },
        type
      }
    )),
    type !== "textList" ? /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("category-list") }, selectedTimes.map((d, i) => {
      return /* @__PURE__ */ import_react.default.createElement("div", { className: "category-item", key: i }, showScheduleName ? /* @__PURE__ */ import_react.default.createElement("div", { className: "category-title" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "category-icon" }, /* @__PURE__ */ import_react.default.createElement("span", null)), /* @__PURE__ */ import_react.default.createElement("span", null, d.name)) : null, showScheduleTime ? /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("time") }, formatDateStr(d.start_at), " - ", formatDateStr(d.end_at)) : null);
    })) : null
  );
};
var scheduleCalendar_default = EventScheduleCalendar;
