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, { useEffect, useMemo, useState } from 'react';
import { CalendarOutlined } from '@ant-design/icons';
import classNames from 'classnames';
import dayjs from 'dayjs';
import { DatePicker, Page } from '@pisell/materials';
import "./index.less";

// 禁用之前的日期
export var disabledDate = function disabledDate(current, position, currentDate) {
  if (position === 'start') {
    return false;
  }
  if (position === 'end' && current.endOf('day') && currentDate !== null && currentDate !== void 0 && currentDate[0]) {
    // 结束日期最大 = 开始日期 + 8个月
    var endDisabled = currentDate === null || currentDate === void 0 ? void 0 : currentDate[0].add(8, 'months').format('YYYY-MM-DD');
    return dayjs(endDisabled) < current.startOf('day') || current.startOf('day') <= (currentDate === null || currentDate === void 0 ? void 0 : currentDate[0]);
  }
  return current && current <= (currentDate === null || currentDate === void 0 ? void 0 : currentDate[0]);
};
var DateRange = function DateRange(_ref) {
  var state = _ref.state,
    dispatch = _ref.dispatch,
    key = _ref.key;
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    value = _useState2[0],
    setValue = _useState2[1];
  useEffect(function () {
    var _state$date, _state$date2;
    var lists = [(_state$date = state.date) === null || _state$date === void 0 ? void 0 : _state$date.value, (_state$date2 = state.date) === null || _state$date2 === void 0 ? void 0 : _state$date2.end_date];
    setValue(lists);
    console.log('lists', lists);
  }, [state.date]);

  /**
   * @title: 点击确定
   * @description:
   * @param {any} e
   * @return {*}
   * @Author: WangHan
   * @Date: 2024-12-12 15:39
   */
  var dateChange = function dateChange(e) {
    dispatch({
      type: 'setDate',
      payload: {
        value: e[0],
        end_date: e[1],
        edit: true
      }
    });
  };

  /**
   * @title: 格式化语言
   * @description:
   * @param {*} useMemo
   * @return {*}
   * @Author: WangHan
   * @Date: 2024-12-09 17:47
   */
  var formatLocale = useMemo(function () {
    var locale = state === null || state === void 0 ? void 0 : state.locale;
    var isEn = locale === 'en' || locale === 'en-US';
    var _format = isEn ? 'DD/MM/YYYY' : 'YYYY-MM-DD';
    return _format;
  }, [state === null || state === void 0 ? void 0 : state.locale, state.date]);
  return useMemo(function () {
    return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
      className: classNames('pisell-lowcode__booking-service-header-date-range', {
        'pisell-lowcode__booking-service-header-date-range-terminal': state.isTerminalDayBookingPage
      })
    }, /*#__PURE__*/React.createElement(Page, null, /*#__PURE__*/React.createElement(DatePicker.RangePicker, {
      key: key,
      inputReadOnly: true,
      allowClear: false,
      size: "large",
      style: {
        width: '100%',
        padding: 0
      },
      suffixIcon: !state.disabledEdit ? /*#__PURE__*/React.createElement(CalendarOutlined, null) : null,
      disabledDate: disabledDate,
      value: [value[0], value[1]],
      onChange: dateChange,
      format: formatLocale,
      isHideCustomSelect: true,
      endDateDays: 1
    }))));
  }, [value, state.disabledEdit, state === null || state === void 0 ? void 0 : state.locale]);
};
export default DateRange;