var _excluded = ["locale", "slice", "timeObjChange", "selectProps", "duration", "usableTime", "startDate", "timeObj"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import { Select } from 'antd';
import React, { useCallback, useEffect, useMemo } from 'react';
import { sliceDayIntoFiveMinutes, getEndDate, flexibleObj, getDiscountTime } from "../../utils";
import dayjs from 'dayjs';
import { locales } from '@pisell/utils';
var Duration = function Duration(props) {
  var _props$locale = props.locale,
    locale = _props$locale === void 0 ? 'en' : _props$locale,
    _props$slice = props.slice,
    slice = _props$slice === void 0 ? 5 : _props$slice,
    timeObjChange = props.timeObjChange,
    selectProps = props.selectProps,
    duration = props.duration,
    usableTime = props.usableTime,
    startDate = props.startDate,
    timeObj = props.timeObj,
    resetProps = _objectWithoutProperties(props, _excluded);
  var startDateStr = useMemo(function () {
    return dayjs(startDate).format('YYYY-MM-DD HH:mm:ss');
  }, [startDate]);

  /**
   * @title: 时间切片
   * @description:
   * @return {*}
   * @Author: zhiwei.Wang
   * @Date: 2024-01-09 13:37
   */
  var times = useMemo(function () {
    if (timeObj) {
      return timeObj.durationSlicesBasedOnTime;
    }
    var _sliceDayIntoFiveMinu = sliceDayIntoFiveMinutes(slice, locale),
      durationSlicesBasedOnTime = _sliceDayIntoFiveMinu.durationSlicesBasedOnTime;
    var _duration = _typeof(duration) === 'object' ? duration.type : duration;
    // 灵活时长
    if (_duration === 'flexible') {
      var endDate = getEndDate(startDate, _duration, usableTime !== null && usableTime !== void 0 && usableTime.end_at ? dayjs(usableTime === null || usableTime === void 0 ? void 0 : usableTime.end_at) : null);
      var timeValue = endDate.diff(startDate, 'minute');

      // 动态获取结束时间
      durationSlicesBasedOnTime.unshift({
        label: locales.getText('pisell2.text.flexible'),
        value: flexibleObj.create(timeValue)
      });
    }
    if (resetProps.value && !durationSlicesBasedOnTime.some(function (d) {
      return d.value === resetProps.value;
    })) {
      durationSlicesBasedOnTime.unshift({
        label: getDiscountTime(resetProps.value),
        value: resetProps.value,
        //@ts-ignore
        disabled: true
      });
    }
    return durationSlicesBasedOnTime;
  }, [timeObj === null || timeObj === void 0 ? void 0 : timeObj.durationSlicesBasedOnTime, slice, locale, duration, startDateStr, usableTime === null || usableTime === void 0 ? void 0 : usableTime.end_at]);
  useEffect(function () {
    timeObjChange === null || timeObjChange === void 0 || timeObjChange(times);
  }, [times]);
  var renderOptions = useCallback(function (array) {
    var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'value';
    var label = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'label';
    return array.map(function (d) {
      return /*#__PURE__*/React.createElement(Select.Option, {
        value: d[value],
        key: d[value],
        disabled: d.disabled
      }, d[label]);
    });
  }, []);
  var options = useMemo(function () {
    console.log('render form options');
    return renderOptions(times);
  }, [times]);
  return /*#__PURE__*/React.createElement(Select, _extends({}, selectProps, resetProps), options);
};
export default Duration;