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/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_materials = require("@pisell/materials");
var import_classnames = __toESM(require("classnames"));
var import_utils = require("@pisell/utils");
var import_resourceUsability = __toESM(require("../resourceUsability"));
var import_mock = require("../../mock");
var import_index = require("./index.less");
var TimeResourceSelector = ({
  timeSlots = import_mock.mock.time_resource || [],
  showUnavailable = true,
  defaultExpanded = true,
  defaultSelectFirst = true,
  onChange,
  className,
  style
}) => {
  const [selectedValues, setSelectedValues] = (0, import_react.useState)([]);
  const [error, setError] = (0, import_react.useState)("");
  const [expanded, setExpanded] = (0, import_react.useState)(defaultExpanded);
  (0, import_react.useEffect)(() => {
    setExpanded(defaultExpanded);
  }, [defaultExpanded]);
  const filteredTimeSlots = timeSlots == null ? void 0 : timeSlots.filter(
    (slot) => showUnavailable ? true : !slot.disabled
  );
  (0, import_react.useEffect)(() => {
    let id = [];
    if (defaultSelectFirst) {
      const firstAvailable = filteredTimeSlots.find((slot) => !slot.disabled);
      if (firstAvailable) {
        id = [firstAvailable.id];
      }
    } else {
      id = [];
    }
    setSelectedValues(id);
    onChange == null ? void 0 : onChange(id);
  }, [defaultSelectFirst]);
  const handleChange = (value) => {
    const newValue = Array.isArray(value) ? value : [value];
    setSelectedValues(newValue);
    onChange == null ? void 0 : onChange(newValue);
  };
  const renderTimeCard = (item) => {
    const obj = {
      ...item,
      selected: selectedValues.includes(item == null ? void 0 : item.id)
    };
    return /* @__PURE__ */ import_react.default.createElement(
      import_resourceUsability.default,
      {
        showRemaining: true,
        showTime: true,
        item: obj,
        onTimeSlotClick: (e) => handleChange(e),
        style: { width: "100%" }
      }
    );
  };
  (0, import_react.useEffect)(() => {
    setError(
      (selectedValues == null ? void 0 : selectedValues.length) === 0 ? "Please select a session time" : ""
    );
  }, [selectedValues]);
  const toggleExpand = () => {
    setExpanded(!expanded);
  };
  const renderContent = () => {
    if ((filteredTimeSlots == null ? void 0 : filteredTimeSlots.length) === 0) {
      return /* @__PURE__ */ import_react.default.createElement("div", { className: "time-resource-no-data" }, "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_materials.PisellCustomCheckboxGroup,
      {
        dataSource: filteredTimeSlots,
        renderItem: renderTimeCard,
        columns: (0, import_utils.isMobile)() ? 2 : 4,
        horizontalGap: 8,
        verticalGap: 8,
        allowAdd: false
      }
    ));
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellCard,
    {
      className: (0, import_classnames.default)("time-resource-selector", className),
      style
    },
    /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: (0, import_classnames.default)("time-resource-header", {
          "time-resource-header-expanded": expanded
        }),
        onClick: toggleExpand
      },
      /* @__PURE__ */ import_react.default.createElement("div", { className: "time-resource-title" }, "选择时间段"),
      /* @__PURE__ */ import_react.default.createElement("div", { className: "time-resource-icon" }, expanded ? /* @__PURE__ */ import_react.default.createElement(import_materials.Iconfont, { type: "pisell2-chevron-up" }) : /* @__PURE__ */ import_react.default.createElement(import_materials.Iconfont, { type: "pisell2-chevron-down" }))
    ),
    /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: (0, import_classnames.default)("time-resource-content", {
          "time-resource-content-expanded": expanded
        })
      },
      renderContent()
    )
  );
};
var timeResourceSelector_default = TimeResourceSelector;
