"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
  };
};

var _a;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.NumberFilterMemo = exports.NumberFilterTypeSet = void 0;

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

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

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

var input_1 = require("../input");

var select_1 = __importDefault(require("../select"));

var button_1 = require("../button");

var popconfirm_1 = __importDefault(require("../popconfirm"));

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

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

var lodash_1 = require("lodash");

var ahooks_1 = require("ahooks");

var components_1 = require("./..");

var en_US_1 = __importDefault(require("./languages/en-US"));

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

var prefixCls = 'dmc-number-filter';
var useCallback = React.useCallback;
var localeMap = (_a = {}, _a[components_1.LocaleEnum.en] = en_US_1["default"].numberFilterLocale, _a[components_1.LocaleEnum.zh] = zh_CN_1["default"].numberFilterLocale, _a);
var NumberFilterTypeSet;

(function (NumberFilterTypeSet) {
  NumberFilterTypeSet["interval"] = "interval";
  NumberFilterTypeSet["segment"] = "segment";
})(NumberFilterTypeSet = exports.NumberFilterTypeSet || (exports.NumberFilterTypeSet = {}));

var NumberItem = React.memo(function (_a) {
  var defaultValue = _a.defaultValue,
      statistic = _a.statistic,
      onChange = _a.onChange,
      label = _a.label,
      placeholder = _a.placeholder;
  return jsx_runtime_1.jsxs("div", __assign({
    className: prefixCls + "-item"
  }, {
    children: [jsx_runtime_1.jsxs("div", __assign({
      className: prefixCls + "-label"
    }, {
      children: [label, ' ', jsx_runtime_1.jsxs("span", __assign({
        title: "" + (statistic || ''),
        className: prefixCls + "-statistic"
      }, {
        children: ["(", statistic, ")"]
      }), void 0)]
    }), void 0), jsx_runtime_1.jsx(input_1.InputNumber, {
      type: "menu-bg",
      size: "compact",
      onChange: onChange,
      placeholder: placeholder,
      visibilityHandler: true,
      defaultValue: defaultValue,
      className: prefixCls + "-input"
    }, void 0)]
  }), void 0);
});

var Content = function Content(props) {
  var _a = React.useContext(components_1.BaseContext).locale,
      locale = _a === void 0 ? components_1.LocaleEnum.zh : _a;
  var type = props.type,
      placeholder = props.placeholder,
      labelMin = props.labelMin,
      statisticMin = props.statisticMin,
      min = props.min,
      labelMax = props.labelMax,
      statisticMax = props.statisticMax,
      max = props.max,
      changeMax = props.changeMax,
      changeMin = props.changeMin,
      segmentation = props.segmentation,
      pushSegmentation = props.pushSegmentation,
      deleteSegmentation = props.deleteSegmentation;
  var newRef = React.useRef({
    max: '',
    min: ''
  });
  var ref = React.useRef(null);

  var _b = React.useState(type),
      _type = _b[0],
      setType = _b[1];

  var _confirm = function _confirm() {
    var _a = newRef.current,
        min = _a.min,
        max = _a.max;

    if (lodash_1.isNumber(Number(min)) && lodash_1.isNumber(Number(max))) {
      pushSegmentation === null || pushSegmentation === void 0 ? void 0 : pushSegmentation({
        min: Number(min),
        max: Number(max)
      });
    }
  };

  var options = ahooks_1.useCreation(function () {
    return [{
      kay: localeMap[locale].intervalScreening,
      label: localeMap[locale].intervalScreening,
      value: localeMap[locale].intervalScreening
    }, {
      kay: localeMap[locale].segmentScreening,
      label: localeMap[locale].segmentScreening,
      value: localeMap[locale].segmentScreening
    }];
  }, []);
  return jsx_runtime_1.jsxs("div", __assign({
    ref: ref
  }, {
    children: [_type && jsx_runtime_1.jsxs("div", __assign({
      style: {
        display: 'flex',
        marginBottom: 8
      }
    }, {
      children: [jsx_runtime_1.jsx(select_1["default"], {
        className: prefixCls + "-select",
        size: "mini",
        type: "menu-bg",
        value: _type === NumberFilterTypeSet.segment ? localeMap[locale].segmentScreening : localeMap[locale].intervalScreening,
        options: options,
        getPopupContainer: function getPopupContainer() {
          return ref.current;
        },
        onChange: function onChange(value) {
          var _value = NumberFilterTypeSet.interval;

          if (value === localeMap[locale].segmentScreening) {
            _value = NumberFilterTypeSet.segment;
          }

          setType(_value);
        }
      }, void 0), _type === NumberFilterTypeSet.segment && jsx_runtime_1.jsx(popconfirm_1["default"], __assign({
        description: jsx_runtime_1.jsxs("div", __assign({
          style: {
            display: 'flex'
          }
        }, {
          children: [jsx_runtime_1.jsx(NumberItem, {
            placeholder: placeholder,
            label: labelMin,
            statistic: statisticMin,
            onChange: function onChange(e) {
              newRef.current.min = e;
            }
          }, void 0), jsx_runtime_1.jsx("div", __assign({
            className: prefixCls + "-split"
          }, {
            children: "-"
          }), void 0), jsx_runtime_1.jsx(NumberItem, {
            label: labelMax,
            statistic: statisticMax,
            onChange: function onChange(e) {
              newRef.current.max = e;
            },
            placeholder: placeholder
          }, void 0)]
        }), void 0),
        onConfirm: function onConfirm() {
          _confirm();
        },
        okText: localeMap[locale].ok,
        cancelText: localeMap[locale].cancel,
        message: localeMap[locale].addSegment,
        emotion: 'info',
        placement: "bottom",
        trigger: "click",
        getPopupContainer: function getPopupContainer() {
          return ref.current;
        }
      }, {
        children: jsx_runtime_1.jsx(button_1.IconButton, {
          icon: "add",
          type: "only-icon"
        }, void 0)
      }), void 0)]
    }), void 0), (_type === NumberFilterTypeSet.interval || _type === undefined) && jsx_runtime_1.jsxs("div", __assign({
      className: prefixCls + "-content"
    }, {
      children: [jsx_runtime_1.jsx(NumberItem, {
        placeholder: placeholder,
        label: labelMin,
        statistic: statisticMin,
        defaultValue: min,
        onChange: changeMin
      }, void 0), jsx_runtime_1.jsx("div", __assign({
        className: prefixCls + "-split"
      }, {
        children: "-"
      }), void 0), jsx_runtime_1.jsx(NumberItem, {
        label: labelMax,
        statistic: statisticMax,
        defaultValue: max,
        onChange: changeMax,
        placeholder: placeholder
      }, void 0)]
    }), void 0), _type === NumberFilterTypeSet.segment && jsx_runtime_1.jsxs("div", __assign({
      className: prefixCls + "-content",
      style: {
        display: 'block'
      }
    }, {
      children: [lodash_1.size(segmentation) === 0 && jsx_runtime_1.jsxs("div", __assign({
        className: prefixCls + "-none-text"
      }, {
        children: [jsx_runtime_1.jsx("div", {
          children: localeMap[locale].noContent
        }, void 0), jsx_runtime_1.jsxs("div", {
          children: [localeMap[locale].please, jsx_runtime_1.jsxs("span", {
            children: [statisticMin, "-", statisticMax, localeMap[locale].between]
          }, void 0), localeMap[locale].addSegment]
        }, void 0)]
      }), void 0), lodash_1.map(segmentation, function (seg, index) {
        return jsx_runtime_1.jsxs("div", __assign({
          className: prefixCls + "-content-item"
        }, {
          children: [jsx_runtime_1.jsxs("div", {
            children: [seg.min, "-", seg.max]
          }, void 0), jsx_runtime_1.jsx(button_1.LinkButton, __assign({
            onClick: function onClick() {
              deleteSegmentation === null || deleteSegmentation === void 0 ? void 0 : deleteSegmentation(index);
            }
          }, {
            children: localeMap[locale]["delete"]
          }), void 0)]
        }), index);
      })]
    }), void 0)]
  }), void 0);
};

var NumberFilter = function NumberFilter(props) {
  var _a = props,
      min = _a.min,
      max = _a.max,
      onOk = _a.onOk,
      onClose = _a.onClose,
      children = _a.children,
      className = _a.className,
      labelMax = _a.labelMax,
      labelMin = _a.labelMin,
      statisticMin = _a.statisticMin,
      statisticMax = _a.statisticMax,
      placeholder = _a.placeholder,
      type = _a.type,
      // 区间｜分段  不传，就是老版本区间
  segmentation = _a.segmentation,
      restProps = __rest(_a, ["min", "max", "onOk", "onClose", "children", "className", "labelMax", "labelMin", "statisticMin", "statisticMax", "placeholder", "type", "segmentation"]);

  var _b = ahooks_1.useSetState({
    min: min,
    max: max,
    segmentation: segmentation || []
  }),
      state = _b[0],
      setState = _b[1];

  React.useEffect(function () {
    setState({
      segmentation: segmentation
    });
  }, [segmentation, setState]);
  var changeMin = ahooks_1.useMemoizedFn(function (v) {
    setState({
      min: v
    });
  });
  var changeMax = ahooks_1.useMemoizedFn(function (v) {
    setState({
      max: v
    });
  });
  var pushSegmentation = ahooks_1.useMemoizedFn(function (seg) {
    var segmentation = state.segmentation;
    segmentation === null || segmentation === void 0 ? void 0 : segmentation.push(seg);
    setState({
      segmentation: segmentation
    });
  });
  var deleteSegmentation = ahooks_1.useMemoizedFn(function (index) {
    var segmentation = state.segmentation;
    segmentation === null || segmentation === void 0 ? void 0 : segmentation.splice(index, 1);
    setState({
      segmentation: segmentation
    });
  });
  var handleConfirm = useCallback(function () {
    if (onOk) {
      var current = state;
      var result = {
        min: current.min,
        max: current.max,
        segmentation: current.segmentation
      };
      onOk(result);
    }
  }, [onOk, state]);
  var handleClose = useCallback(function () {
    if (onClose) {
      onClose();
    }

    setState({
      min: min,
      max: max
    });
  }, [onClose, min, max, setState]);
  return jsx_runtime_1.jsx(CreateFilter_1["default"], __assign({
    onClose: handleClose,
    onOk: handleConfirm,
    overlay: jsx_runtime_1.jsx(Content, {
      type: type,
      placeholder: placeholder,
      labelMin: labelMin,
      statisticMin: statisticMin,
      min: min,
      changeMin: changeMin,
      labelMax: labelMax,
      statisticMax: statisticMax,
      max: max,
      changeMax: changeMax,
      segmentation: state.segmentation,
      pushSegmentation: pushSegmentation,
      deleteSegmentation: deleteSegmentation
    }, void 0),
    footerClassName: prefixCls + "-footer",
    className: classnames_1["default"](prefixCls, className)
  }, restProps, {
    children: children
  }), void 0);
};

NumberFilter.displayName = 'NumberFilter';
exports["default"] = NumberFilter;
var defaultProps = {
  placement: 'bottomLeft'
};
NumberFilter.defaultProps = defaultProps;
var NumberFilterMemo = React.memo(NumberFilter);
exports.NumberFilterMemo = NumberFilterMemo;
NumberFilterMemo.displayName = 'NumberFilterMemo';

