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 isoWeek from 'dayjs/plugin/isoWeek';
import classNames from 'classnames';
import CreateFilter from "./CreateFilter";
import { getDateCustomerIncludeTitle, getDateCustomerOption, getDateWithFormat, getDurationOfFastType // translateUnitToFormat,
} from "./__private__/helper";
import zhCn from "./languages/zh-CN";
import { DateUnitKeys, DynamincType, FastTypeKeys, DateUntilKeys, showNature } 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 } from "../date-picker";
dayjs.extend(quarterOfYear);
dayjs.extend(isoWeek);
dayjs.locale();
export { TimeFormat };
export var prefixCls = 'dmc-date-filter';
var FORMAT = 'YYYY-MM-DD HH:mm:ss';
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,
      _d = _b.include,
      inc = _d === void 0 ? true : _d,
      _e = _b.type,
      type = _e === void 0 ? DynamincType.before : _e,
      _f = _b.unit,
      unit = _f === void 0 ? DateUnitKeys.day : _f,
      _g = _b.untilValue,
      untilValue = _g === void 0 ? DateUntilKeys.today : _g,
      _h = _b.nature,
      nature = _h === void 0 ? false : _h;

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

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

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

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

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

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

  var _p = useState(inc),
      include = _p[0],
      setInclude = _p[1];

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

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

  var _s = useState(unit),
      dynamicUnit = _s[0],
      setDynamicUnit = _s[1];

  var _t = useState(nature),
      dynamicNature = _t[0],
      setDynamicNature = _t[1];

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


  var _v = useState(false),
      parentVisible = _v[0],
      setParentVisible = _v[1]; // 组件内状态，用来控制picker展开是显示的format(展开时不需要format)


  var _w = useState(true),
      needStartDateFilterFormat = _w[0],
      setNeedStartDateFilterFormat = _w[1];

  var _x = useState(true),
      needEndDateFilterFormat = _x[0],
      setNeedEndDateFilterFormat = _x[1];

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

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

      if (fastType === FastTypeKeys.customer) {
        config.value = dynamicVal;
        config.type = dynamicType;
        config.unit = dynamicUnit;
        config.untilValue = dynamicUntilValue;
        config.include = include;
        config.nature = dynamicNature;
      }

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

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

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

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

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

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

    var _a = getDurationOfFastType(fast, val, type, unit, fastMap, until, include, unitInclude),
        fastTypeStart = _a[0],
        fastTypeEnd = _a[1]; // const tf = translateUnitToFormat(unit);
    // setStartFormat(tf);
    // setEndFormat(tf);


    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, dynamicUntilValue, include, dynamicNature);
    }
  }, [changeDateByFastType, dynamicVal, dynamicType, dynamicUnit, dynamicUntilValue, include, dynamicNature]);
  var onChangeDynamicVal = useCallback(function (val) {
    setDynamicVal(val);
    changeDateByFastType(fastType, val, dynamicType, dynamicUnit, dynamicUntilValue, include, dynamicNature);
  }, [changeDateByFastType, fastType, dynamicType, dynamicUnit, dynamicUntilValue, include, dynamicNature]);
  var onChangeDynamicType = useCallback(function (val) {
    setDynamicType(val);
    setInclude(true);
    var untilVal = showNature(dynamicUnit) ? DateUntilKeys.today : DateUntilKeys.now;
    setDynamicUntilValue(untilVal);
    changeDateByFastType(fastType, dynamicVal, val, dynamicUnit, untilVal, true, dynamicNature);
  }, [changeDateByFastType, fastType, dynamicVal, dynamicUnit, dynamicNature]);
  var onChangeDynamicUnit = useCallback(function (val) {
    var untilVal = showNature(val) ? DateUntilKeys.today : DateUntilKeys.now;
    setDynamicUnit(val);
    setDynamicUntilValue(untilVal);
    setDynamicNature(false);
    setInclude(true);
    changeDateByFastType(fastType, dynamicVal, dynamicType, val, untilVal, true, false);
  }, [changeDateByFastType, fastType, dynamicVal, dynamicType]);
  var onChangeDynamicNature = useCallback(function (val) {
    setDynamicNature(val);
    setInclude(true);
    var untilVal = showNature(dynamicUnit) ? DateUntilKeys.today : DateUntilKeys.now;
    setDynamicUntilValue(untilVal);
    changeDateByFastType(fastType, dynamicVal, dynamicType, dynamicUnit, untilVal, true, val);
  }, [changeDateByFastType, dynamicType, dynamicUnit, dynamicVal, fastType]);
  var onUntilValueChange = useCallback(function (val) {
    setDynamicUntilValue(val);
    changeDateByFastType(fastType, dynamicVal, dynamicType, dynamicUnit, val, include, dynamicNature);
  }, [changeDateByFastType, dynamicNature, dynamicType, dynamicUnit, dynamicVal, fastType, include]);
  var onIncludeChange = useCallback(function (val) {
    setInclude(val);
    changeDateByFastType(fastType, dynamicVal, dynamicType, dynamicUnit, dynamicUntilValue, val, dynamicNature);
  }, [changeDateByFastType, dynamicNature, dynamicType, dynamicUnit, dynamicUntilValue, dynamicVal, fastType]);
  var handleClose = useCallback(function () {
    setStartDate(start ? dayjs(start) : null);
    setEndDate(end ? dayjs(end) : null);
    setInclude(inc);
    setDynamicUnit(unit);
    setDynamicType(type);
    setDynamicVal(num);
    setFastType(fast);
    setDynamicNature(nature);

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

        if (start !== null && fastType === FastTypeKeys.untilNow) {
          setStartDate(start ? dayjs(start) : null);
        }
      }
    } // eslint-disable-next-line react-hooks/exhaustive-deps

  }, [// changeDateByFastType,
  // dynamicType,
  // dynamicUnit,
  // dynamicNature,
  // dynamicVal,
  // end,
  fastType, parentVisible, start]);
  var _y = locale.filterLocale,
      labelStart = _y.labelStart,
      labelEnd = _y.labelEnd,
      labelFastType = _y.labelFastType,
      untilNow = _y.untilNow;
  var isCustomer = fastType === FastTypeKeys.customer;
  var isFater = dynamicType === DynamincType.after;
  var checkNature = showNature(dynamicUnit);
  var showStartFooter = isCustomer && isFater;
  var showEndFooter = isCustomer && !isFater;
  var checkboxDisabled = checkNature && !dynamicNature;
  var natureIncludeControl = include || checkNature && !dynamicNature && !include || checkNature && dynamicNature && include;

  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,
      nature: dynamicNature,
      onNumChange: onChangeDynamicVal,
      onTypeChange: onChangeDynamicType,
      onUnitChange: onChangeDynamicUnit,
      onNatureChange: onChangeDynamicNature,
      getPopupContainer: getPopupContainer
    }, void 0), _jsxs("div", __assign({
      className: prefixCls + "-date-wrap"
    }, {
      children: [_jsx(DateItem, __assign({
        dropdownAlign: {
          points: ['tl', 'bl'],
          offset: [0, 0],
          targetOffset: [0, 0],
          overflow: {
            adjustX: false,
            adjustY: false
          }
        }
      }, startProps, {
        timeFormat: startFormat,
        min: min,
        label: labelStart,
        value: startDate,
        fastType: fastType,
        include: include,
        labelInclude: getDateCustomerIncludeTitle(locale, dynamicUnit),
        onFormatChange: startDateSelect,
        onChange: startDateChange,
        onCheck: onIncludeChange,
        getPopupContainer: getPopupContainer,
        showCheckbox: showStartFooter,
        checkboxDisabled: checkboxDisabled,
        showUntil: showStartFooter && natureIncludeControl,
        untilValue: dynamicUntilValue,
        untilOptions: getDateCustomerOption(locale, dynamicUnit, dynamicNature, dynamicType),
        untilTitle: untilNow,
        onUntilValueChange: onUntilValueChange,
        onOpenChange: onStartOpenChange,
        format: needStartDateFilterFormat ? FORMAT : undefined
      }), void 0), _jsx("span", __assign({
        className: prefixCls + "-date-split"
      }, {
        children: "-"
      }), void 0), _jsx(DateItem, __assign({
        dropdownAlign: {
          points: ['tr', 'br'],
          offset: [0, 0],
          targetOffset: [0, 0],
          overflow: {
            adjustX: false,
            adjustY: false
          }
        }
      }, endProps, {
        timeFormat: endFormat,
        max: max,
        label: labelEnd,
        value: endDate,
        fastType: fastType,
        include: include,
        labelInclude: getDateCustomerIncludeTitle(locale, dynamicUnit),
        onFormatChange: endDateSelect,
        onChange: endDateChange,
        onCheck: onIncludeChange,
        getPopupContainer: getPopupContainer,
        showCheckbox: showEndFooter,
        checkboxDisabled: checkboxDisabled,
        showUntil: showEndFooter && natureIncludeControl,
        untilValue: dynamicUntilValue,
        untilOptions: getDateCustomerOption(locale, dynamicUnit, dynamicNature, dynamicType),
        untilTitle: untilNow,
        onUntilValueChange: onUntilValueChange,
        onOpenChange: onEndOpenChange,
        format: needEndDateFilterFormat ? FORMAT : undefined
      }), 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 };

