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/TimeResourceSelector/index.tsx
var TimeResourceSelector_exports = {};
__export(TimeResourceSelector_exports, {
  default: () => TimeResourceSelector_default
});
module.exports = __toCommonJS(TimeResourceSelector_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_materials = require("@pisell/materials");
var import_PisellCustomCheckboxGroup = __toESM(require("../PisellCustomCheckboxGroup"));
var import_index = require("./index.less");
var TimeResourceSelector = ({
  timeSlots = [],
  multiple = false,
  showUnavailable = true,
  defaultExpanded = true,
  defaultSelectFirst = true,
  onChange,
  className,
  style
}) => {
  const [selectedValues, setSelectedValues] = (0, import_react.useState)([]);
  const [error, setError] = (0, import_react.useState)("");
  const filteredTimeSlots = timeSlots.filter(
    (slot) => showUnavailable ? true : !slot.disabled
  );
  (0, import_react.useEffect)(() => {
    if (defaultSelectFirst && filteredTimeSlots.length > 0 && selectedValues.length === 0) {
      const firstAvailable = filteredTimeSlots.find((slot) => !slot.disabled);
      if (firstAvailable) {
        const value = firstAvailable.value;
        setSelectedValues([value]);
        onChange == null ? void 0 : onChange(multiple ? [value] : value);
      }
    }
  }, [filteredTimeSlots, defaultSelectFirst, multiple, onChange]);
  const renderTimeCard = (item) => {
    const { time, remainingCount, disabled } = item;
    return /* @__PURE__ */ import_react.default.createElement(
      import_materials.PisellCard,
      {
        className: "time-resource-card",
        bodyStyle: { padding: "12px" }
      },
      /* @__PURE__ */ import_react.default.createElement("div", { className: "time-slot-content" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "time-text" }, time), /* @__PURE__ */ import_react.default.createElement("div", { className: "remaining-count" }, "剩余: ", remainingCount))
    );
  };
  const handleChange = (0, import_react.useCallback)(
    (value) => {
      if (Array.isArray(value)) {
        setSelectedValues(value);
        setError(value.length === 0 ? "Please select a session time" : "");
      } else {
        setSelectedValues([value]);
        setError("");
      }
      onChange == null ? void 0 : onChange(value);
    },
    [onChange]
  );
  const renderContent = () => {
    if (filteredTimeSlots.length === 0) {
      return /* @__PURE__ */ import_react.default.createElement(import_antd.Empty, { description: "No dates are available" });
    }
    return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, error && /* @__PURE__ */ import_react.default.createElement("div", { className: "time-resource-error" }, error), /* @__PURE__ */ import_react.default.createElement(
      import_PisellCustomCheckboxGroup.default,
      {
        dataSource: filteredTimeSlots,
        renderItem: renderTimeCard,
        value: selectedValues,
        onChange: handleChange,
        isMultiple: multiple,
        columns: 4,
        horizontalGap: 16,
        verticalGap: 16
      }
    ));
  };
  return /* @__PURE__ */ import_react.default.createElement("div", { className: `time-resource-selector ${className || ""}`, style }, /* @__PURE__ */ import_react.default.createElement(import_antd.Collapse, { defaultActiveKey: defaultExpanded ? ["1"] : [] }, /* @__PURE__ */ import_react.default.createElement(import_antd.Collapse.Panel, { header: "选择时间段", key: "1" }, renderContent())));
};
var TimeResourceSelector_default = TimeResourceSelector;
