function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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, useEffect } from 'react';
import { PisellCard, PisellCustomCheckboxGroup, Iconfont } from '@pisell/materials';
import classNames from 'classnames';
import { isMobile } from '@pisell/utils';
import ResourceUsability from "../resourceUsability";
import { mock } from "../../mock";
import "./index.less";
var TimeResourceSelector = function TimeResourceSelector(_ref) {
  var _ref$timeSlots = _ref.timeSlots,
    timeSlots = _ref$timeSlots === void 0 ? mock.time_resource || [] : _ref$timeSlots,
    _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 _useState5 = useState(defaultExpanded),
    _useState6 = _slicedToArray(_useState5, 2),
    expanded = _useState6[0],
    setExpanded = _useState6[1];
  useEffect(function () {
    setExpanded(defaultExpanded);
  }, [defaultExpanded]);

  // 过滤可用的时间段
  var filteredTimeSlots = timeSlots === null || timeSlots === void 0 ? void 0 : timeSlots.filter(function (slot) {
    return showUnavailable ? true : !slot.disabled;
  });
  useEffect(function () {
    var id = [];
    if (defaultSelectFirst) {
      var firstAvailable = filteredTimeSlots.find(function (slot) {
        return !slot.disabled;
      });
      if (firstAvailable) {
        id = [firstAvailable.id];
      }
    } else {
      id = [];
    }
    setSelectedValues(id);
    onChange === null || onChange === void 0 || onChange(id);
  }, [defaultSelectFirst]);

  /**
   * @title: 点击卡片
   * @description:
   * @param {string} value
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-01-11 16:27
   */
  var handleChange = function handleChange(value) {
    var newValue = Array.isArray(value) ? value : [value];
    setSelectedValues(newValue);
    onChange === null || onChange === void 0 || onChange(newValue);
  };

  /**
   * @title: 渲染时间卡片
   * @description:
   * @param {TimeSlot} item
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-01-10 18:08
   */
  var renderTimeCard = function renderTimeCard(item) {
    var obj = _objectSpread(_objectSpread({}, item), {}, {
      selected: selectedValues.includes(item === null || item === void 0 ? void 0 : item.id)
    });
    return /*#__PURE__*/React.createElement(ResourceUsability, {
      showRemaining: true,
      showTime: true,
      item: obj,
      onTimeSlotClick: function onTimeSlotClick(e) {
        return handleChange(e);
      },
      style: {
        width: '100%'
      }
    });
  };
  useEffect(function () {
    setError((selectedValues === null || selectedValues === void 0 ? void 0 : selectedValues.length) === 0 ? 'Please select a session time' : '');
  }, [selectedValues]);

  /**
   * @title: 展开/收起
   * @description:
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-01-10 19:00
   */
  var toggleExpand = function toggleExpand() {
    setExpanded(!expanded);
  };

  /**
   * @title: 渲染内容
   * @description:
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-01-10 18:17
   */
  var renderContent = function renderContent() {
    // 没有可用时间段
    if ((filteredTimeSlots === null || filteredTimeSlots === void 0 ? void 0 : filteredTimeSlots.length) === 0) {
      return /*#__PURE__*/React.createElement("div", {
        className: "time-resource-no-data"
      }, "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,
      columns: isMobile() ? 2 : 4,
      horizontalGap: 8,
      verticalGap: 8,
      allowAdd: false
    }));
  };
  return /*#__PURE__*/React.createElement(PisellCard, {
    className: classNames('time-resource-selector', className),
    style: style
  }, /*#__PURE__*/React.createElement("div", {
    className: classNames('time-resource-header', {
      'time-resource-header-expanded': expanded
    }),
    onClick: toggleExpand
  }, /*#__PURE__*/React.createElement("div", {
    className: "time-resource-title"
  }, "\u9009\u62E9\u65F6\u95F4\u6BB5"), /*#__PURE__*/React.createElement("div", {
    className: "time-resource-icon"
  }, expanded ? /*#__PURE__*/React.createElement(Iconfont, {
    type: "pisell2-chevron-up"
  }) : /*#__PURE__*/React.createElement(Iconfont, {
    type: "pisell2-chevron-down"
  }))), /*#__PURE__*/React.createElement("div", {
    className: classNames('time-resource-content', {
      'time-resource-content-expanded': expanded
    })
  }, renderContent()));
};
export default TimeResourceSelector;