var __assign = this && this.__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

var __rest = this && this.__rest || function (s, e) {
  var t = {};

  for (var p in s) {
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  }

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};

import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from 'react';
import dayjs from 'dayjs';
import quarterOfYear from 'dayjs/plugin/quarterOfYear';
import classNames from 'classnames';
import CreateFilter from "./CreateFilter";
import { getDurationOfFastType } from "./__private__/helper";
import zhCn from "./languages/zh-CN";
import { DateUnitKeys, DynamincType, FastTypeKeys } from "./__private__/constant";
import FastTypes from "./__private__/FastTypes";
import DateItem from "./__private__/DateItem";
import DynamicSelect from "./__private__/DynamicSelect";
import "./style/date-filter.css";
import { TimeFormat, timeFormatValue } from "../date-picker";
dayjs.extend(quarterOfYear);
dayjs.locale();
export var prefixCls = 'dmc-date-filter';
var useState = React.useState,
    useCallback = React.useCallback,
    useEffect = React.useEffect;

var DateFilter = function DateFilter(props) {
  var _a = props,
      locale = _a.locale,
      className = _a.className,
      children = _a.children,
      onOk = _a.onOk,
      onClose = _a.onClose,
      value = _a.value,
      min = _a.min,
      max = _a.max,
      restProps = __rest(_a, ["locale", "className", "children", "onOk", "onClose", "value", "min", "max"]);

  var _b = value || {},
      _c = _b.fastType,
      fast = _c === void 0 ? FastTypeKeys.unlimited : _c,
      start = _b.start,
      end = _b.end,
      sf = _b.startFormat,
      ef = _b.endFormat,
      startProps = _b.startProps,
      endProps = _b.endProps,
      num = _b.value,
      include = _b.includeToday,
      _d = _b.type,
      type = _d === void 0 ? DynamincType.after : _d,
      _e = _b.unit,
      unit = _e === void 0 ? DateUnitKeys.day : _e;

  var ref = React.useRef(null);
  var getPopupContainer = React.useCallback(function () {
    return ref.current;
  }, []);

  var _f = useState(fast),
      fastType = _f[0],
      setFastType = _f[1];

  var _g = useState(start ? dayjs(start) : null),
      startDate = _g[0],
      setStartDate = _g[1];

  var _h = useState(end ? dayjs(end) : null),
      endDate = _h[0],
      setEndDate = _h[1];

  var _j = useState(sf !== null && sf !== void 0 ? sf : TimeFormat.no),
      startFormat = _j[0],
      setStartFormat = _j[1];

  var _k = useState(ef !== null && ef !== void 0 ? ef : TimeFormat.no),
      endFormat = _k[0],
      setEndFormat = _k[1];

  var _l = useState(include),
      includeToday = _l[0],
      setIncludeToday = _l[1];

  var _m = useState(num),
      dynamicVal = _m[0],
      setDynamicVal = _m[1];

  var _o = useState(type),
      dynamicType = _o[0],
      setDynamicType = _o[1];

  var _p = useState(unit),
      dynamicUnit = _p[0],
      setDynamicUnit = _p[1]; // 这个visible是根据CreateFilter的钩子函数来得到的状态，并不是内部组件的显示隐藏状态！


  var _q = useState(false),
      parentVisible = _q[0],
      setParentVisible = _q[1];

  useEffect(function () {
    setFastType(fast);
    setStartDate(start ? dayjs(start) : null);
    setEndDate(end ? dayjs(end) : null);
    setIncludeToday(include);
    setDynamicVal(num);
    setDynamicType(type);
    setDynamicUnit(unit);
  }, [fast, start, end, include, num, type, unit]);
  useEffect(function () {
    setStartFormat(startFormat);
    setEndFormat(endFormat);
  }, [startFormat, endFormat]);
  var handleConfim = useCallback(function () {
    if (onOk) {
      var config = {
        includeToday: includeToday
      };

      if (fastType) {
        config.fastType = fastType;
      }

      if (fastType === FastTypeKeys.customer) {
        config.value = dynamicVal;
        config.type = dynamicType;
        config.unit = dynamicUnit;
      }

      if (startDate) {
        config.start = dayjs(startDate).format(timeFormatValue(startFormat));
        config.startMoment = startDate;
      }

      if (endDate) {
        config.end = dayjs(endDate).format(timeFormatValue(endFormat));
        config.endMoment = endDate;
      }

      if (startFormat) {
        config.startFormat = startFormat;
      }

      if (endFormat) {
        config.endFormat = endFormat;
      }

      onOk(config);
    }
  }, [onOk, includeToday, fastType, startDate, endDate, dynamicVal, dynamicType, dynamicUnit, startFormat, endFormat]);
  var fastMap = React.useMemo(function () {
    var _a;

    return _a = {}, _a[FastTypeKeys.customer] = [null, null], _a[FastTypeKeys.unlimited] = [null, null], _a[FastTypeKeys.untilNow] = [null, dayjs().endOf('day')], _a[FastTypeKeys.tomorrow] = [dayjs().add(1, 'days').startOf('day'), dayjs().add(1, 'days').endOf('day')], _a[FastTypeKeys.last24Hours] = [dayjs().subtract(24, 'hours'), dayjs()], _a[FastTypeKeys.yesterday] = [dayjs().subtract(1, 'days').startOf('day'), dayjs().subtract(1, 'days').endOf('day')], _a[FastTypeKeys.lastWeek] = [dayjs().subtract(1, 'weeks').startOf('week'), dayjs().subtract(1, 'weeks').endOf('week')], _a[FastTypeKeys.lastMonth] = [dayjs().subtract(1, 'months').startOf('month'), dayjs().subtract(1, 'months').endOf('month')], _a[FastTypeKeys.lastQuarter] = [dayjs().subtract(1, 'quarters').startOf('quarter'), dayjs().subtract(1, 'quarters').endOf('quarter')], _a[FastTypeKeys.lastYear] = [dayjs().subtract(1, 'years').startOf('year'), dayjs().subtract(1, 'years').endOf('year')], _a[FastTypeKeys.today] = [dayjs().startOf('day'), dayjs().endOf('day')], _a[FastTypeKeys.thisWeek] = [dayjs().startOf('week'), dayjs().endOf('week')], _a[FastTypeKeys.thisMonth] = [dayjs().startOf('month'), dayjs().endOf('month')], _a[FastTypeKeys.thisQuarter] = [dayjs().startOf('quarter'), dayjs().endOf('quarter')], _a[FastTypeKeys.thisYear] = [dayjs().startOf('year'), dayjs().endOf('year')], _a[FastTypeKeys.future24Hours] = [dayjs(), dayjs().add(24, 'hours')], _a[FastTypeKeys.nextWeek] = [dayjs().add(1, 'weeks').startOf('week'), dayjs().add(1, 'weeks').endOf('week')], _a; // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [parentVisible]);
  var changeDateByFastType = useCallback(function (fast, val, type, unit) {
    if (fast === FastTypeKeys.unlimited) {
      setStartDate(start ? dayjs(start) : null);
      setEndDate(end ? dayjs(end) : null);
      return;
    }

    var _a = getDurationOfFastType(fast, val, type, unit, fastMap),
        fastTypeStart = _a[0],
        fastTypeEnd = _a[1];

    setStartDate(fastTypeStart);
    setEndDate(fastTypeEnd);
  }, [fastMap, start, end]);
  var onChangeFast = useCallback(function (info) {
    var key = info.key;

    if (key) {
      setFastType(key);
      changeDateByFastType(key, dynamicVal, dynamicType, dynamicUnit);
    }
  }, [changeDateByFastType, dynamicVal, dynamicType, dynamicUnit]);
  var onChangeDynamicVal = useCallback(function (val) {
    setDynamicVal(val);
    changeDateByFastType(fastType, val, dynamicType, dynamicUnit);
  }, [changeDateByFastType, fastType, dynamicType, dynamicUnit]);
  var onChangeDynamicType = useCallback(function (val) {
    setDynamicType(val);
    changeDateByFastType(fastType, dynamicVal, val, dynamicUnit);
  }, [changeDateByFastType, fastType, dynamicVal, dynamicUnit]);
  var onChangeDynamicUnit = useCallback(function (val) {
    setDynamicUnit(val);
    changeDateByFastType(fastType, dynamicVal, dynamicType, val);
  }, [changeDateByFastType, fastType, dynamicVal, dynamicType]);
  var handleClose = useCallback(function () {
    setStartDate(start ? dayjs(start) : null);
    setEndDate(end ? dayjs(end) : null);
    setIncludeToday(include);
    setDynamicUnit(unit);
    setDynamicType(type);
    setDynamicVal(num);
    setFastType(fast);

    if (onClose) {
      onClose();
    }
  }, [onClose, start, end, include, num, type, unit, fast]);
  var startDateChange = useCallback(function (date, _dateString, timeFormat) {
    if (fastType !== FastTypeKeys.untilNow) setFastType(FastTypeKeys.unlimited);
    setStartDate(date);
    setStartFormat(timeFormat);
  }, [fastType]);
  var startDateSelect = useCallback(function (timeFormat) {
    setStartFormat(timeFormat);
  }, []);
  var endDateSelect = useCallback(function (timeFormat) {
    setEndFormat(timeFormat);
  }, []);
  var endDateChange = useCallback(function (date, _dateString, timeFormat) {
    setFastType(FastTypeKeys.unlimited);
    setEndDate(date);
    setEndFormat(timeFormat);
  }, []);
  var onVisibleChange = useCallback(function (visible) {
    setParentVisible(visible);
  }, []);
  useEffect(function () {
    if (parentVisible) {
      if (fastType && fastType !== FastTypeKeys.unlimited) {
        changeDateByFastType(fastType, dynamicVal, dynamicType, dynamicUnit); // 预增逻辑: 当选过开始时间后，startDate跟随选中的时间来走

        if (start !== null && fastType === FastTypeKeys.untilNow) {
          setStartDate(start ? dayjs(start) : null);
        }
      }
    }
  }, [changeDateByFastType, dynamicType, dynamicUnit, dynamicVal, end, fastType, parentVisible, start]);
  var _r = locale.filterLocale,
      labelStart = _r.labelStart,
      labelIncludeToday = _r.labelIncludeToday,
      labelEnd = _r.labelEnd,
      labelFastType = _r.labelFastType;
  var isCustomer = fastType === FastTypeKeys.customer;
  var isFater = dynamicType === DynamincType.after;

  var overlay = _jsxs("div", __assign({
    ref: ref,
    className: "" + prefixCls
  }, {
    children: [_jsx(FastTypes, {
      locale: locale,
      value: fastType,
      onClickMenuItem: onChangeFast,
      labelFastType: labelFastType,
      getPopupContainer: getPopupContainer
    }, void 0), isCustomer && _jsx(DynamicSelect, {
      locale: locale,
      unit: dynamicUnit,
      type: dynamicType,
      dynamicVal: dynamicVal,
      onNumChange: onChangeDynamicVal,
      onTypeChange: onChangeDynamicType,
      onUnitChange: onChangeDynamicUnit,
      getPopupContainer: getPopupContainer
    }, void 0), _jsxs("div", __assign({
      className: prefixCls + "-date-wrap"
    }, {
      children: [_jsx(DateItem, __assign({}, startProps, {
        initialTimeFormat: startFormat,
        min: min,
        locale: locale,
        label: labelStart,
        value: startDate,
        fastType: fastType,
        includeToday: includeToday,
        onFormatChange: startDateSelect,
        onChange: startDateChange,
        onCheck: setIncludeToday,
        labelIncludeToday: labelIncludeToday,
        getPopupContainer: getPopupContainer,
        showCheckbox: isCustomer && isFater
      }), void 0), _jsx("span", __assign({
        className: prefixCls + "-date-split"
      }, {
        children: "-"
      }), void 0), _jsx(DateItem, __assign({}, endProps, {
        initialTimeFormat: endFormat,
        max: max,
        locale: locale,
        label: labelEnd,
        value: endDate,
        fastType: fastType,
        includeToday: includeToday,
        onFormatChange: endDateSelect,
        onChange: endDateChange,
        onCheck: setIncludeToday,
        labelIncludeToday: labelIncludeToday,
        getPopupContainer: getPopupContainer,
        showCheckbox: isCustomer && !isFater
      }), void 0)]
    }), void 0)]
  }), void 0);

  return _jsx(CreateFilter, __assign({}, restProps, {
    onOk: handleConfim,
    onClose: handleClose,
    className: classNames(prefixCls, className),
    footerClassName: prefixCls + "-footer",
    overlay: overlay,
    onVisibleChange: onVisibleChange
  }, {
    children: children
  }), void 0);
};

DateFilter.displayName = 'DateFilter';
export default DateFilter;
var defaultProps = {
  placement: 'bottomLeft',
  locale: zhCn
};
DateFilter.defaultProps = defaultProps;
var DateFilterMemo = /*#__PURE__*/React.memo(DateFilter);
DateFilterMemo.displayName = 'DateFilterMemo';
export { DateFilterMemo };

