"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 __spreadArray = void 0 && (void 0).__spreadArray || function (to, from) {
  for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) {
    to[j] = from[i];
  }

  return to;
};

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

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

var react_1 = require("react");

var jsx_runtime_1 = require("react/jsx-runtime");
/* eslint-disable @typescript-eslint/no-explicit-any */


var react_2 = require("react");

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

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

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

var icon_1 = __importDefault(require("../../icon"));

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

var FilterTypes_1 = __importDefault(require("./FilterTypes"));

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

var lodash_1 = require("lodash");

var prefixCls = 'dmc-text-filter';
var initialFilterData = {
  type: constant_1.FilterTypeKeys.contain,
  value: undefined
};

var splitData = function splitData(data, key, slot) {
  if (slot === void 0) {
    slot = constant_1.KEY_SLOT;
  }

  var ds = lodash_1.cloneDeep(data);

  var _a = key.split(slot),
      andCount = _a[0],
      orCount = _a[1];

  var orArray = ds[~~andCount];
  var currentOr = orArray[~~orCount];
  return {
    andCount: andCount,
    orCount: orCount,
    orArray: orArray,
    currentOr: currentOr,
    ds: ds
  };
};

var FilterListCellType;

(function (FilterListCellType) {
  FilterListCellType["or"] = "or";
  FilterListCellType["both"] = "both";
})(FilterListCellType || (FilterListCellType = {}));

var FilterListCell = function FilterListCell(props) {
  var _a, _b, _c;

  var value = props.value,
      orTitle = props.orTitle,
      disabled = props.disabled,
      closeable = props.closeable,
      extra = props.extra,
      searchOptions = props.searchOptions,
      locale = props.locale,
      onAndClick = props.onAndClick,
      onOrClick = props.onOrClick,
      onMenuClick = props.onMenuClick,
      onSelect = props.onSelect,
      onClose = props.onClose,
      id = props.id;
  var type = value.type,
      filterValue = value.value; // 多选

  var isMuti = [constant_1.FilterTypeKeys.eitherof, constant_1.FilterTypeKeys.neitherof].includes(type); // 无选项

  var isDisabled = [constant_1.FilterTypeKeys.empty, constant_1.FilterTypeKeys.unempty].includes(type); // 输入框

  var isInput = [constant_1.FilterTypeKeys.contain, constant_1.FilterTypeKeys.nocontain, constant_1.FilterTypeKeys.startwith, constant_1.FilterTypeKeys.endwith].includes(type);
  var ref = react_2.useRef(null);
  var getPopupContainer = react_2.useCallback(function () {
    return ref.current;
  }, []);

  var _onMenuClick = react_2.useCallback(function (info) {
    onMenuClick && onMenuClick(info, id);
  }, [id, onMenuClick]);

  var _andClick = react_2.useCallback(function () {
    onAndClick && onAndClick(id);
  }, [id, onAndClick]);

  var _orClick = react_2.useCallback(function () {
    onOrClick && onOrClick(id);
  }, [id, onOrClick]);

  var _onClose = react_2.useCallback(function () {
    onClose && onClose(id);
  }, [id, onClose]);

  var _onSelect = react_2.useCallback(function (info) {
    onSelect && onSelect(info, id);
  }, [id, onSelect]);

  var onChange = react_2.useCallback(function (e) {
    onSelect && onSelect(e.target.value.trim(), id);
  }, [id, onSelect]);
  return jsx_runtime_1.jsxs("div", __assign({
    className: prefixCls + "-filter-list-cell",
    ref: ref
  }, {
    children: [orTitle && jsx_runtime_1.jsx("span", __assign({
      className: prefixCls + "-filter-list-cell-text"
    }, {
      children: "\u6216"
    }), void 0), jsx_runtime_1.jsx(FilterTypes_1["default"], {
      value: type,
      locale: locale,
      onClickMenuItem: _onMenuClick,
      getPopupContainer: getPopupContainer
    }, void 0), isInput ? jsx_runtime_1.jsx(input_1["default"], {
      className: classnames_1["default"]((_a = {}, _a[prefixCls + "-filter-list-cell-disabled"] = isDisabled, _a[prefixCls + "-filter-list-cell-width"] = orTitle, _a)),
      value: filterValue,
      type: "menu-bg",
      size: "compact",
      disabled: isDisabled || disabled,
      onChange: onChange
    }, void 0) : jsx_runtime_1.jsx(select_1["default"], {
      className: classnames_1["default"]((_b = {}, _b[prefixCls + "-filter-list-cell-disabled"] = isDisabled, _b[prefixCls + "-filter-list-cell-width"] = orTitle, _b)),
      mode: isMuti ? 'multiple' : undefined,
      type: "menu-bg",
      size: "compact",
      showArrow: false,
      showSearch: true,
      disabled: isDisabled || disabled,
      options: searchOptions,
      getPopupContainer: getPopupContainer,
      onChange: _onSelect,
      value: filterValue
    }, void 0), jsx_runtime_1.jsx(icon_1["default"], {
      icon: "close",
      className: classnames_1["default"]((_c = {}, _c[prefixCls + "-filter-list-cell-icon"] = true, _c[prefixCls + "-filter-list-cell-icon-none"] = !closeable, _c)),
      onClick: _onClose
    }, void 0), extra && extra === FilterListCellType.both && jsx_runtime_1.jsxs("span", __assign({
      className: prefixCls + "-filter-list-cell-extra"
    }, {
      children: [jsx_runtime_1.jsx(button_1.LinkButton, __assign({
        size: "compact",
        onClick: _andClick
      }, {
        children: "\u4E14"
      }), void 0), "&", jsx_runtime_1.jsx(button_1.LinkButton, __assign({
        size: "compact",
        onClick: _orClick
      }, {
        children: "\u6216"
      }), void 0)]
    }), void 0), extra && extra === FilterListCellType.or && jsx_runtime_1.jsx("span", __assign({
      className: prefixCls + "-filter-list-cell-extra"
    }, {
      children: jsx_runtime_1.jsx(button_1.LinkButton, __assign({
        size: "compact",
        onClick: _orClick
      }, {
        children: "\u6216"
      }), void 0)
    }), void 0)]
  }), void 0);
};

exports.FilterListCell = FilterListCell;

var FilterList = function FilterList(props) {
  var searchOptions = props.searchOptions,
      locale = props.locale,
      _a = props.dataSource,
      dataSource = _a === void 0 ? [[initialFilterData]] : _a,
      onChange = props.onChange;
  var cellProps = {
    searchOptions: searchOptions,
    locale: locale
  };
  var onAndClick = react_2.useCallback(function () {
    onChange && onChange(__spreadArray(__spreadArray([], dataSource), [[initialFilterData]]));
  }, [dataSource, onChange]);
  var onOrClick = react_2.useCallback(function (key) {
    var _a = splitData(dataSource, key),
        ds = _a.ds,
        andCount = _a.andCount;

    var insert = ds[~~andCount].concat([initialFilterData]);
    var newDataSource = lodash_1.fill(ds, insert, ~~andCount, ~~andCount + 1);
    onChange && onChange(newDataSource);
  }, [dataSource, onChange]);
  var onClose = react_2.useCallback(function (key) {
    var _a = splitData(dataSource, key),
        ds = _a.ds,
        andCount = _a.andCount,
        orCount = _a.orCount;

    var orArray = ds[~~andCount];
    var newDataSource;

    if (orArray.length <= 1) {
      newDataSource = lodash_1.pull(ds, orArray);
    } else {
      var newOrArray = lodash_1.pull(ds[~~andCount], ds[~~andCount][~~orCount]);
      newDataSource = lodash_1.fill(ds, newOrArray, ~~andCount, ~~andCount + 1);
    }

    onChange && onChange(newDataSource);
  }, [dataSource, onChange]);
  var onMenuClick = react_2.useCallback(function (info, key) {
    var _a = splitData(dataSource, key),
        ds = _a.ds,
        andCount = _a.andCount,
        orCount = _a.orCount,
        orArray = _a.orArray,
        currentOr = _a.currentOr;

    var currentOrArray = lodash_1.fill(orArray, __assign(__assign({}, currentOr), {
      type: info.key,
      value: undefined
    }), ~~orCount, ~~orCount + 1);
    onChange && onChange(lodash_1.fill(ds, currentOrArray, ~~andCount, ~~andCount + 1));
  }, [dataSource, onChange]);
  var onSelect = react_2.useCallback(function (value, key) {
    var _a = splitData(dataSource, key),
        ds = _a.ds,
        andCount = _a.andCount,
        orCount = _a.orCount,
        orArray = _a.orArray,
        currentOr = _a.currentOr;

    var currentOrArray = lodash_1.fill(orArray, __assign(__assign({}, currentOr), {
      value: value
    }), ~~orCount, ~~orCount + 1);
    onChange && onChange(lodash_1.fill(ds, currentOrArray, ~~andCount, ~~andCount + 1));
  }, [dataSource, onChange]);
  return jsx_runtime_1.jsx("div", __assign({
    className: prefixCls + "-filter-list"
  }, {
    children: dataSource.map(function (orArray, andCount) {
      return jsx_runtime_1.jsxs(react_2.Fragment, {
        children: [orArray.map(function (info, i) {
          var key = "" + andCount + constant_1.KEY_SLOT + i;
          var orTitle = i !== 0;
          var extra = dataSource.length === andCount + 1 && orArray.length === i + 1 ? FilterListCellType.both : orArray.length === 1 || orArray.length === i + 1 ? FilterListCellType.or : undefined;
          return react_1.createElement(exports.FilterListCell, __assign({}, cellProps, {
            value: info,
            orTitle: orTitle,
            extra: extra,
            closeable: !(dataSource.length === 1 && orArray.length === 1),
            key: key,
            id: key,
            onAndClick: onAndClick,
            onOrClick: onOrClick,
            onClose: onClose,
            onMenuClick: onMenuClick,
            onSelect: onSelect
          }));
        }), dataSource.length !== andCount + 1 && jsx_runtime_1.jsxs("div", __assign({
          className: prefixCls + "-filter-list-divider"
        }, {
          children: ["\u4E14", jsx_runtime_1.jsx("span", {}, void 0)]
        }), void 0)]
      }, andCount);
    })
  }), void 0);
};

exports["default"] = FilterList;

