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, { forwardRef, useImperativeHandle, useState, useRef } from 'react';
import classNames from 'classnames';
import dayjs from 'dayjs';
import { Iconfont } from '@pisell/materials';
import Status from "../CalendarDate/Status";
import CalendarWeek from "../CalendarDate/CalenDateWeek";
import CalendarDate from "../CalendarDate";
import { mock } from "../../../../mock";
import OpenCard from "../../../OpenCard";
// import { getProductAvailableCount } from '../../serve';
import "./index.less";
var SelectDate = function SelectDate(_ref, ref) {
  var currentDate = _ref.currentDate,
    detail = _ref.detail,
    currentDateChange = _ref.currentDateChange,
    setSurplusDisabledChange = _ref.setSurplusDisabledChange,
    cutTime = _ref.cutTime,
    setCutTime = _ref.setCutTime,
    showCalendarPopup = _ref.showCalendarPopup;
  // 缓存剩余量数据
  var _useState = useState(mock.availability_calendar.limitData),
    _useState2 = _slicedToArray(_useState, 2),
    dateLimitData = _useState2[0],
    setDateLimitData = _useState2[1];
  var calendarDateRef = useRef(null);
  useImperativeHandle(ref, function () {
    return {};
  });

  // const _getProductAvailableCount = useRequest(getProductAvailableCount, {
  //   manual: true,
  //   onSuccess: (res) => {
  //     const data = res?.data || [];
  //     const dates = data?.dates || [];
  //     const dateData = { ...dateLimitData };
  //     dates.forEach((item: any) => {
  //       dateData[item.date] = item;
  //     });
  //     setDateLimitData(dateData);

  //     const cutTimeData = {
  //       shop_time: data?.shop_time,
  //       ...data?.cut_off_time,
  //     };
  //     setCutTime(cutTimeData);
  //     // 这里加个校验 如果当天时间不可用, 自动寻找一个可用的时间
  //     // try {
  //     //   const { status, disabled } = getStatus(
  //     //     dateData,
  //     //     currentDate,
  //     //     cutTimeData
  //     //   );
  //     //   if (disabled || status === 'sold_out') {
  //     //     for (let key in dateData) {
  //     //       const { status, disabled } = getStatus(dateData, key, cutTimeData);
  //     //       if (!disabled && status !== 'sold_out') {
  //     //         currentDateChange(key);
  //     //         return;
  //     //       }
  //     //     }
  //     //   }
  //     // } catch (err) {}
  //     // const { status, disabled } = getStatus(data, currentDate);
  //     // if (disabled || status === 'sold_out') {
  //     //   setSurplusDisabled(true);
  //     // } else {
  //     //   setSurplusDisabled(false);
  //     // }
  //   },
  // });

  // 当日是否还有剩余量,没有则不加入购物车(此处为了处理以前遗留逻辑问题)
  // useEffect(() => {
  //   const { status, disabled } = getStatus(dateLimitData, currentDate, cutTime);
  //   const isShowCallToBook = getCallToBookStatus(
  //     currentDate,
  //     dateLimitData,
  //     cutTime
  //   );
  //   let surplusDisabledChange = false;
  //   if (
  //     disabled ||
  //     status === 'sold_out' ||
  //     isShowCallToBook
  //     // ||
  //     // _getProductAvailableCount.loading
  //   ) {
  //     surplusDisabledChange = true;
  //   } else {
  //     surplusDisabledChange = false;
  //   }
  //   setSurplusDisabledChange(surplusDisabledChange);
  // }, [currentDate, dateLimitData, cutTime]);
  // _getProductAvailableCount.loading,

  var getLimitData = function getLimitData(start_date, end_date) {
    // 列举start_date至end_date的全部日期
    var _list = [];
    var start = dayjs(start_date);
    var end = dayjs(end_date);
    while (start.isBefore(end)) {
      _list.push(start.format('YYYY-MM-DD'));
      start.add(1, 'days');
    }
    console.log('dateLimitData', dateLimitData);
    console.log('_list', _list);
    // 判断是否有全部缓存数据
    var haveAllData = _list.find(function (item) {
      return !dateLimitData[item];
    });
    if (haveAllData) {
      // _getProductAvailableCount.run(detail.id, {
      //   start_date: start_date,
      //   end_date: end_date,
      // });
    }
  };

  /**
   * @title: 头部额外内容
   * @description:
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-01-14 14:52
   */
  var extraHeader = function extraHeader() {
    return /*#__PURE__*/React.createElement("div", {
      onClick: function onClick(e) {
        var _calendarDateRef$curr;
        e.stopPropagation();
        calendarDateRef === null || calendarDateRef === void 0 || (_calendarDateRef$curr = calendarDateRef.current) === null || _calendarDateRef$curr === void 0 || _calendarDateRef$curr.open(currentDate, detail === null || detail === void 0 ? void 0 : detail.id);
      }
    }, /*#__PURE__*/React.createElement(Iconfont, {
      type: "pisell2-calendar",
      className: classNames('calendar-icon')
    }));
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(OpenCard, {
    title: 'Select a date',
    initOpen: true,
    showArrow: false,
    isClick: false,
    extra: showCalendarPopup ? extraHeader() : null
  }, /*#__PURE__*/React.createElement(CalendarWeek
  // dateChange={(date) => {
  //   currentDateChange(date);
  // }}
  , {
    currentDate: currentDate,
    productId: detail === null || detail === void 0 ? void 0 : detail.id,
    cutTime: cutTime
    // getLimitData={getLimitData}
    ,
    dateLimitData: dateLimitData
  }), /*#__PURE__*/React.createElement(Status, null)), /*#__PURE__*/React.createElement(CalendarDate, {
    ref: calendarDateRef
    // dateChange={(date: string) => {
    //   currentDateChange(date);
    // }}
    ,
    cutTime: cutTime,
    dateLimitData: dateLimitData
    // getLimitData={getLimitData}
    // loading={_getProductAvailableCount.loading}
  }));
};
export default /*#__PURE__*/forwardRef(SelectDate);