function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useState, useCallback, useEffect } from 'react';
import { Collapse, Empty } from 'antd';
import { PisellCard } from '@pisell/materials';
import PisellCustomCheckboxGroup from "../PisellCustomCheckboxGroup";
import "./index.less";
var TimeResourceSelector = function TimeResourceSelector(_ref) {
  var _ref$timeSlots = _ref.timeSlots,
    timeSlots = _ref$timeSlots === void 0 ? [] : _ref$timeSlots,
    _ref$multiple = _ref.multiple,
    multiple = _ref$multiple === void 0 ? false : _ref$multiple,
    _ref$showUnavailable = _ref.showUnavailable,
    showUnavailable = _ref$showUnavailable === void 0 ? true : _ref$showUnavailable,
    _ref$defaultExpanded = _ref.defaultExpanded,
    defaultExpanded = _ref$defaultExpanded === void 0 ? true : _ref$defaultExpanded,
    _ref$defaultSelectFir = _ref.defaultSelectFirst,
    defaultSelectFirst = _ref$defaultSelectFir === void 0 ? true : _ref$defaultSelectFir,
    onChange = _ref.onChange,
    className = _ref.className,
    style = _ref.style;
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    selectedValues = _useState2[0],
    setSelectedValues = _useState2[1];
  var _useState3 = useState(''),
    _useState4 = _slicedToArray(_useState3, 2),
    error = _useState4[0],
    setError = _useState4[1];

  // 过滤可用的时间段
  var filteredTimeSlots = timeSlots.filter(function (slot) {
    return showUnavailable ? true : !slot.disabled;
  });

  // 初始化默认选中第一个可用选项
  useEffect(function () {
    if (defaultSelectFirst && filteredTimeSlots.length > 0 && selectedValues.length === 0) {
      var firstAvailable = filteredTimeSlots.find(function (slot) {
        return !slot.disabled;
      });
      if (firstAvailable) {
        var value = firstAvailable.value;
        setSelectedValues([value]);
        onChange === null || onChange === void 0 || onChange(multiple ? [value] : value);
      }
    }
  }, [filteredTimeSlots, defaultSelectFirst, multiple, onChange]);

  // 渲染时间卡片
  var renderTimeCard = function renderTimeCard(item) {
    var time = item.time,
      remainingCount = item.remainingCount,
      disabled = item.disabled;
    return /*#__PURE__*/React.createElement(PisellCard, {
      className: "time-resource-card",
      bodyStyle: {
        padding: '12px'
      }
    }, /*#__PURE__*/React.createElement("div", {
      className: "time-slot-content"
    }, /*#__PURE__*/React.createElement("div", {
      className: "time-text"
    }, time), /*#__PURE__*/React.createElement("div", {
      className: "remaining-count"
    }, "\u5269\u4F59: ", remainingCount)));
  };

  // 处理选择变更
  var handleChange = useCallback(function (value) {
    if (Array.isArray(value)) {
      setSelectedValues(value);
      setError(value.length === 0 ? 'Please select a session time' : '');
    } else {
      setSelectedValues([value]);
      setError('');
    }
    onChange === null || onChange === void 0 || onChange(value);
  }, [onChange]);

  // 渲染内容
  var renderContent = function renderContent() {
    if (filteredTimeSlots.length === 0) {
      return /*#__PURE__*/React.createElement(Empty, {
        description: "No dates are available"
      });
    }
    return /*#__PURE__*/React.createElement(React.Fragment, null, error && /*#__PURE__*/React.createElement("div", {
      className: "time-resource-error"
    }, error), /*#__PURE__*/React.createElement(PisellCustomCheckboxGroup, {
      dataSource: filteredTimeSlots,
      renderItem: renderTimeCard,
      value: selectedValues,
      onChange: handleChange,
      isMultiple: multiple,
      columns: 4,
      horizontalGap: 16,
      verticalGap: 16
    }));
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "time-resource-selector ".concat(className || ''),
    style: style
  }, /*#__PURE__*/React.createElement(Collapse, {
    defaultActiveKey: defaultExpanded ? ['1'] : []
  }, /*#__PURE__*/React.createElement(Collapse.Panel, {
    header: "\u9009\u62E9\u65F6\u95F4\u6BB5",
    key: "1"
  }, renderContent())));
};
export default TimeResourceSelector;