"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 = 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 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");

dayjs_1["default"].extend(quarterOfYear_1["default"]);
dayjs_1["default"].locale();
exports.prefixCls = 'dmc-date-filter';
var useState = React.useState,
    useCallback = React.useCallback,
    useEffect = React.useEffect;
var DATE_FORMATTER = 'YYYY-MM-DD';

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 || {},
      fast = _b.fastType,
      start = _b.start,
      end = _b.end,
      num = _b.value,
      include = _b.includeToday,
      _c = _b.type,
      type = _c === void 0 ? constant_1.DynamincType.after : _c,
      _d = _b.unit,
      unit = _d === void 0 ? constant_1.DateUnitKeys.day : _d;

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

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

  var _f = useState(start ? dayjs_1["default"](start) : null),
      startDate = _f[0],
      setStartDate = _f[1];

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

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

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

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

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


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

  var handleConfim = useCallback(function () {
    if (onOk) {
      var config = {
        includeToday: includeToday
      };

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

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

      if (startDate) {
        config.start = dayjs_1["default"](startDate).format(DATE_FORMATTER);
        config.startMoment = startDate;
      }

      if (endDate) {
        config.end = dayjs_1["default"](endDate).format(DATE_FORMATTER);
        config.endMoment = endDate;
      }

      onOk(config);
    }
  }, [onOk, includeToday, startDate, endDate, fastType, dynamicVal, dynamicType, dynamicUnit]);
  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"]().endOf('day')], _a[constant_1.FastTypeKeys.tomorrow] = [dayjs_1["default"]().add(1, 'days').startOf('day'), dayjs_1["default"]().add(1, 'days').endOf('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"]().subtract(1, 'days').endOf('day')], _a[constant_1.FastTypeKeys.lastWeek] = [dayjs_1["default"]().subtract(1, 'weeks').startOf('week'), dayjs_1["default"]().subtract(1, 'weeks').endOf('week')], _a[constant_1.FastTypeKeys.lastMonth] = [dayjs_1["default"]().subtract(1, 'months').startOf('month'), dayjs_1["default"]().subtract(1, 'months').endOf('month')], _a[constant_1.FastTypeKeys.lastQuarter] = [dayjs_1["default"]().subtract(1, 'quarters').startOf('quarter'), dayjs_1["default"]().subtract(1, 'quarters').endOf('quarter')], _a[constant_1.FastTypeKeys.lastYear] = [dayjs_1["default"]().subtract(1, 'years').startOf('year'), dayjs_1["default"]().subtract(1, 'years').endOf('year')], _a[constant_1.FastTypeKeys.today] = [dayjs_1["default"]().startOf('day'), dayjs_1["default"]().endOf('day')], _a[constant_1.FastTypeKeys.thisWeek] = [dayjs_1["default"]().startOf('week'), dayjs_1["default"]().endOf('week')], _a[constant_1.FastTypeKeys.thisMonth] = [dayjs_1["default"]().startOf('month'), dayjs_1["default"]().endOf('month')], _a[constant_1.FastTypeKeys.thisQuarter] = [dayjs_1["default"]().startOf('quarter'), dayjs_1["default"]().endOf('quarter')], _a[constant_1.FastTypeKeys.thisYear] = [dayjs_1["default"]().startOf('year'), dayjs_1["default"]().endOf('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'), dayjs_1["default"]().add(1, 'weeks').endOf('week')], _a; // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [parentVisible]);
  var changeDateByFastType = useCallback(function (fast, val, type, unit) {
    var _a = helper_1.getDurationOfFastType(fast, val, type, unit, fastMap),
        start = _a[0],
        end = _a[1];

    setStartDate(start);
    setEndDate(end);
  }, [fastMap]);
  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_1["default"](start) : null);
    setEndDate(end ? dayjs_1["default"](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) {
    setFastType(undefined);
    setStartDate(date);
  }, []);
  var endDateChange = useCallback(function (date) {
    setFastType(undefined);
    setEndDate(date);
  }, []);
  var onVisibleChange = useCallback(function (visible) {
    setParentVisible(visible);
  }, []);
  useEffect(function () {
    if (parentVisible) {
      if (fastType && fastType !== constant_1.FastTypeKeys.unlimited) {
        changeDateByFastType(fastType, dynamicVal, dynamicType, dynamicUnit);
      } else {
        if (start !== null) {
          setStartDate(start ? dayjs_1["default"](start) : null);
        }

        if (end !== null) {
          setEndDate(end ? dayjs_1["default"](end) : null);
        }
      }
    }
  }, [changeDateByFastType, dynamicType, dynamicUnit, dynamicVal, end, fastType, parentVisible, start]);
  var _o = locale.filterLocale,
      labelStart = _o.labelStart,
      labelIncludeToday = _o.labelIncludeToday,
      labelEnd = _o.labelEnd,
      labelFastType = _o.labelFastType;
  var isCustomer = fastType === constant_1.FastTypeKeys.customer;
  var isFater = dynamicType === constant_1.DynamincType.after;
  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,
      onNumChange: onChangeDynamicVal,
      onTypeChange: onChangeDynamicType,
      onUnitChange: onChangeDynamicUnit,
      getPopupContainer: getPopupContainer
    }, void 0), jsx_runtime_1.jsxs("div", __assign({
      className: exports.prefixCls + "-date-wrap"
    }, {
      children: [jsx_runtime_1.jsx(DateItem_1["default"], {
        min: min,
        locale: locale,
        label: labelStart,
        value: startDate,
        fastType: fastType,
        includeToday: includeToday,
        onChange: startDateChange,
        onCheck: setIncludeToday,
        labelIncludeToday: labelIncludeToday,
        getPopupContainer: getPopupContainer,
        showCheckbox: isCustomer && isFater
      }, void 0), jsx_runtime_1.jsx("span", __assign({
        className: exports.prefixCls + "-date-split"
      }, {
        children: "-"
      }), void 0), jsx_runtime_1.jsx(DateItem_1["default"], {
        max: max,
        locale: locale,
        label: labelEnd,
        value: endDate,
        fastType: fastType,
        includeToday: includeToday,
        onChange: endDateChange,
        onCheck: setIncludeToday,
        labelIncludeToday: labelIncludeToday,
        getPopupContainer: getPopupContainer,
        showCheckbox: isCustomer && !isFater
      }, 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';

