"use strict";

var __assign = void 0 && (void 0).__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 __createBinding = void 0 && (void 0).__createBinding || (Object.create ? function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  Object.defineProperty(o, k2, {
    enumerable: true,
    get: function get() {
      return m[k];
    }
  });
} : function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  o[k2] = m[k];
});

var __setModuleDefault = void 0 && (void 0).__setModuleDefault || (Object.create ? function (o, v) {
  Object.defineProperty(o, "default", {
    enumerable: true,
    value: v
  });
} : function (o, v) {
  o["default"] = v;
});

var __importStar = void 0 && (void 0).__importStar || function (mod) {
  if (mod && mod.__esModule) return mod;
  var result = {};
  if (mod != null) for (var k in mod) {
    if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  }

  __setModuleDefault(result, mod);

  return result;
};

var __rest = void 0 && (void 0).__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;
};

var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.DateFilterMemo = exports.prefixCls = exports.TimeFormat = void 0;

var jsx_runtime_1 = require("react/jsx-runtime");

var React = __importStar(require("react"));

var dayjs_1 = __importDefault(require("dayjs"));

var quarterOfYear_1 = __importDefault(require("dayjs/plugin/quarterOfYear"));

var isoWeek_1 = __importDefault(require("dayjs/plugin/isoWeek"));

var classnames_1 = __importDefault(require("classnames"));

var CreateFilter_1 = __importDefault(require("./CreateFilter"));

var helper_1 = require("./__private__/helper");

var zh_CN_1 = __importDefault(require("./languages/zh-CN"));

var constant_1 = require("./__private__/constant");

var FastTypes_1 = __importDefault(require("./__private__/FastTypes"));

var DateItem_1 = __importDefault(require("./__private__/DateItem"));

var DynamicSelect_1 = __importDefault(require("./__private__/DynamicSelect"));

require("./style/date-filter.css");

var date_picker_1 = require("../date-picker");

Object.defineProperty(exports, "TimeFormat", {
  enumerable: true,
  get: function get() {
    return date_picker_1.TimeFormat;
  }
});
dayjs_1["default"].extend(quarterOfYear_1["default"]);
dayjs_1["default"].extend(isoWeek_1["default"]);
dayjs_1["default"].locale();
exports.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 ? constant_1.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 ? constant_1.DynamincType.before : _e,
      _f = _b.unit,
      unit = _f === void 0 ? constant_1.DateUnitKeys.day : _f,
      _g = _b.untilValue,
      untilValue = _g === void 0 ? constant_1.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_1["default"](start) : null),
      startDate = _k[0],
      setStartDate = _k[1];

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

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

  var _o = useState(ef !== null && ef !== void 0 ? ef : date_picker_1.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_1["default"](start) : null);
    setEndDate(end ? dayjs_1["default"](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 === constant_1.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_1["default"](startDate).format(FORMAT);
        config.startMoment = startDate;
      }

      if (endDate) {
        config.end = dayjs_1["default"](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[constant_1.FastTypeKeys.customer] = [null, null], _a[constant_1.FastTypeKeys.unlimited] = [null, null], _a[constant_1.FastTypeKeys.untilNow] = [null, dayjs_1["default"]().add(1, 'days').startOf('day')], _a[constant_1.FastTypeKeys.tomorrow] = [dayjs_1["default"]().add(1, 'days').startOf('day'), dayjs_1["default"]().add(2, 'days').startOf('day')], _a[constant_1.FastTypeKeys.last24Hours] = [dayjs_1["default"]().subtract(24, 'hours'), dayjs_1["default"]()], _a[constant_1.FastTypeKeys.yesterday] = [dayjs_1["default"]().subtract(1, 'days').startOf('day'), dayjs_1["default"]().startOf('day')], _a[constant_1.FastTypeKeys.lastWeek] = [dayjs_1["default"]().subtract(1, 'weeks').startOf('isoWeek'), dayjs_1["default"]().startOf('isoWeek')], _a[constant_1.FastTypeKeys.lastMonth] = [dayjs_1["default"]().subtract(1, 'months').startOf('month'), dayjs_1["default"]().startOf('month')], _a[constant_1.FastTypeKeys.lastQuarter] = [dayjs_1["default"]().subtract(1, 'quarters').startOf('quarter'), dayjs_1["default"]().startOf('quarter')], _a[constant_1.FastTypeKeys.lastYear] = [dayjs_1["default"]().subtract(1, 'years').startOf('year'), dayjs_1["default"]().startOf('year')], _a[constant_1.FastTypeKeys.today] = [dayjs_1["default"]().startOf('day'), dayjs_1["default"]().add(1, 'days').startOf('day')], _a[constant_1.FastTypeKeys.thisWeek] = [dayjs_1["default"]().startOf('isoWeek'), dayjs_1["default"]().add(1, 'weeks').startOf('isoWeek')], _a[constant_1.FastTypeKeys.thisMonth] = [dayjs_1["default"]().startOf('month'), dayjs_1["default"]().add(1, 'months').startOf('month')], _a[constant_1.FastTypeKeys.thisQuarter] = [dayjs_1["default"]().startOf('quarter'), dayjs_1["default"]().add(1, 'quarters').startOf('quarter')], _a[constant_1.FastTypeKeys.thisYear] = [dayjs_1["default"]().startOf('year'), dayjs_1["default"]().add(1, 'years').startOf('year')], _a[constant_1.FastTypeKeys.future24Hours] = [dayjs_1["default"](), dayjs_1["default"]().add(24, 'hours')], _a[constant_1.FastTypeKeys.nextWeek] = [dayjs_1["default"]().add(1, 'weeks').startOf('week').add(1, 'days'), dayjs_1["default"]().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 === constant_1.FastTypeKeys.unlimited) {
      setStartDate(start ? dayjs_1["default"](start) : null);
      setEndDate(end ? dayjs_1["default"](end) : null);
      return;
    }

    var _a = helper_1.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 = constant_1.showNature(dynamicUnit) ? constant_1.DateUntilKeys.today : constant_1.DateUntilKeys.now;
    setDynamicUntilValue(untilVal);
    changeDateByFastType(fastType, dynamicVal, val, dynamicUnit, untilVal, true, dynamicNature);
  }, [changeDateByFastType, fastType, dynamicVal, dynamicUnit, dynamicNature]);
  var onChangeDynamicUnit = useCallback(function (val) {
    var untilVal = constant_1.showNature(val) ? constant_1.DateUntilKeys.today : constant_1.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 = constant_1.showNature(dynamicUnit) ? constant_1.DateUntilKeys.today : constant_1.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_1["default"](start) : null);
    setEndDate(end ? dayjs_1["default"](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 !== constant_1.FastTypeKeys.untilNow) setFastType(constant_1.FastTypeKeys.unlimited);
    var newDate = date ? helper_1.getDateWithFormat(date, timeFormat) : null;
    setStartDate(newDate);
    setStartFormat(timeFormat);
  }, [fastType]);
  var startDateSelect = useCallback(function (timeFormat) {
    var newDate = startDate && helper_1.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 && helper_1.getDateWithFormat(endDate, timeFormat);
    newDate && setEndDate(newDate);
    setEndFormat(timeFormat);
  }, [endDate]);
  var endDateChange = useCallback(function (date, _dateString, timeFormat) {
    setFastType(constant_1.FastTypeKeys.unlimited);
    var newDate = date ? helper_1.getDateWithFormat(date, timeFormat) : null;
    setEndDate(newDate);
    setEndFormat(timeFormat);
  }, []);
  var onVisibleChange = useCallback(function (visible) {
    setParentVisible(visible);
  }, []);
  useEffect(function () {
    if (parentVisible) {
      if (fastType && fastType !== constant_1.FastTypeKeys.unlimited) {
        changeDateByFastType(fastType, dynamicVal, dynamicType, dynamicUnit, dynamicUntilValue, include, dynamicNature); // 预增逻辑: 当选过开始时间后，startDate跟随选中的时间来走

        if (start !== null && fastType === constant_1.FastTypeKeys.untilNow) {
          setStartDate(start ? dayjs_1["default"](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 === constant_1.FastTypeKeys.customer;
  var isFater = dynamicType === constant_1.DynamincType.after;
  var checkNature = constant_1.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 = jsx_runtime_1.jsxs("div", __assign({
    ref: ref,
    className: "" + exports.prefixCls
  }, {
    children: [jsx_runtime_1.jsx(FastTypes_1["default"], {
      locale: locale,
      value: fastType,
      onClickMenuItem: onChangeFast,
      labelFastType: labelFastType,
      getPopupContainer: getPopupContainer
    }, void 0), isCustomer && jsx_runtime_1.jsx(DynamicSelect_1["default"], {
      locale: locale,
      unit: dynamicUnit,
      type: dynamicType,
      dynamicVal: dynamicVal,
      nature: dynamicNature,
      onNumChange: onChangeDynamicVal,
      onTypeChange: onChangeDynamicType,
      onUnitChange: onChangeDynamicUnit,
      onNatureChange: onChangeDynamicNature,
      getPopupContainer: getPopupContainer
    }, void 0), jsx_runtime_1.jsxs("div", __assign({
      className: exports.prefixCls + "-date-wrap"
    }, {
      children: [jsx_runtime_1.jsx(DateItem_1["default"], __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: helper_1.getDateCustomerIncludeTitle(locale, dynamicUnit),
        onFormatChange: startDateSelect,
        onChange: startDateChange,
        onCheck: onIncludeChange,
        getPopupContainer: getPopupContainer,
        showCheckbox: showStartFooter,
        checkboxDisabled: checkboxDisabled,
        showUntil: showStartFooter && natureIncludeControl,
        untilValue: dynamicUntilValue,
        untilOptions: helper_1.getDateCustomerOption(locale, dynamicUnit, dynamicNature, dynamicType),
        untilTitle: untilNow,
        onUntilValueChange: onUntilValueChange,
        onOpenChange: onStartOpenChange,
        format: needStartDateFilterFormat ? FORMAT : undefined
      }), void 0), jsx_runtime_1.jsx("span", __assign({
        className: exports.prefixCls + "-date-split"
      }, {
        children: "-"
      }), void 0), jsx_runtime_1.jsx(DateItem_1["default"], __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: helper_1.getDateCustomerIncludeTitle(locale, dynamicUnit),
        onFormatChange: endDateSelect,
        onChange: endDateChange,
        onCheck: onIncludeChange,
        getPopupContainer: getPopupContainer,
        showCheckbox: showEndFooter,
        checkboxDisabled: checkboxDisabled,
        showUntil: showEndFooter && natureIncludeControl,
        untilValue: dynamicUntilValue,
        untilOptions: helper_1.getDateCustomerOption(locale, dynamicUnit, dynamicNature, dynamicType),
        untilTitle: untilNow,
        onUntilValueChange: onUntilValueChange,
        onOpenChange: onEndOpenChange,
        format: needEndDateFilterFormat ? FORMAT : undefined
      }), void 0)]
    }), void 0)]
  }), void 0);
  return jsx_runtime_1.jsx(CreateFilter_1["default"], __assign({}, restProps, {
    onOk: handleConfim,
    onClose: handleClose,
    className: classnames_1["default"](exports.prefixCls, className),
    footerClassName: exports.prefixCls + "-footer",
    overlay: overlay,
    onVisibleChange: onVisibleChange
  }, {
    children: children
  }), void 0);
};

DateFilter.displayName = 'DateFilter';
exports["default"] = DateFilter;
var defaultProps = {
  placement: 'bottomLeft',
  locale: zh_CN_1["default"]
};
DateFilter.defaultProps = defaultProps;
var DateFilterMemo = React.memo(DateFilter);
exports.DateFilterMemo = DateFilterMemo;
DateFilterMemo.displayName = 'DateFilterMemo';

