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

  return to;
};

import { createElement as _createElement } from "react";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/* eslint-disable @typescript-eslint/no-explicit-any */

import { useCallback, useRef, Fragment } from 'react';
import classNames from 'classnames';
import { LinkButton } from "../../button";
import Select from "../../select";
import Icon from "../../icon";
import Input from "../../input";
import FilterTypes from "./FilterTypes";
import { FilterTypeKeys, KEY_SLOT } from "./constant";
import { cloneDeep, fill, pull } from 'lodash';
var prefixCls = 'dmc-text-filter';
var initialFilterData = {
  type: FilterTypeKeys.contain,
  value: undefined
};

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

  var ds = 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 = {}));

export 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 = [FilterTypeKeys.eitherof, FilterTypeKeys.neitherof].includes(type); // 无选项

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

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

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

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

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

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

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

  var onChange = useCallback(function (e) {
    onSelect && onSelect(e.target.value.trim(), id);
  }, [id, onSelect]);
  return _jsxs("div", __assign({
    className: prefixCls + "-filter-list-cell",
    ref: ref
  }, {
    children: [orTitle && _jsx("span", __assign({
      className: prefixCls + "-filter-list-cell-text"
    }, {
      children: "\u6216"
    }), void 0), _jsx(FilterTypes, {
      value: type,
      locale: locale,
      onClickMenuItem: _onMenuClick,
      getPopupContainer: getPopupContainer
    }, void 0), isInput ? _jsx(Input, {
      className: classNames((_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(Select, {
      className: classNames((_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(Icon, {
      icon: "close",
      className: classNames((_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 && _jsxs("span", __assign({
      className: prefixCls + "-filter-list-cell-extra"
    }, {
      children: [_jsx(LinkButton, __assign({
        size: "compact",
        onClick: _andClick
      }, {
        children: "\u4E14"
      }), void 0), "&", _jsx(LinkButton, __assign({
        size: "compact",
        onClick: _orClick
      }, {
        children: "\u6216"
      }), void 0)]
    }), void 0), extra && extra === FilterListCellType.or && _jsx("span", __assign({
      className: prefixCls + "-filter-list-cell-extra"
    }, {
      children: _jsx(LinkButton, __assign({
        size: "compact",
        onClick: _orClick
      }, {
        children: "\u6216"
      }), void 0)
    }), void 0)]
  }), void 0);
};

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 = useCallback(function () {
    onChange && onChange(__spreadArray(__spreadArray([], dataSource), [[initialFilterData]]));
  }, [dataSource, onChange]);
  var onOrClick = useCallback(function (key) {
    var _a = splitData(dataSource, key),
        ds = _a.ds,
        andCount = _a.andCount;

    var insert = ds[~~andCount].concat([initialFilterData]);
    var newDataSource = fill(ds, insert, ~~andCount, ~~andCount + 1);
    onChange && onChange(newDataSource);
  }, [dataSource, onChange]);
  var onClose = 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 = pull(ds, orArray);
    } else {
      var newOrArray = pull(ds[~~andCount], ds[~~andCount][~~orCount]);
      newDataSource = fill(ds, newOrArray, ~~andCount, ~~andCount + 1);
    }

    onChange && onChange(newDataSource);
  }, [dataSource, onChange]);
  var onMenuClick = 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 = fill(orArray, __assign(__assign({}, currentOr), {
      type: info.key,
      value: undefined
    }), ~~orCount, ~~orCount + 1);
    onChange && onChange(fill(ds, currentOrArray, ~~andCount, ~~andCount + 1));
  }, [dataSource, onChange]);
  var onSelect = 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 = fill(orArray, __assign(__assign({}, currentOr), {
      value: value
    }), ~~orCount, ~~orCount + 1);
    onChange && onChange(fill(ds, currentOrArray, ~~andCount, ~~andCount + 1));
  }, [dataSource, onChange]);
  return _jsx("div", __assign({
    className: prefixCls + "-filter-list"
  }, {
    children: dataSource.map(function (orArray, andCount) {
      return _jsxs(Fragment, {
        children: [orArray.map(function (info, i) {
          var key = "" + andCount + 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 /*#__PURE__*/_createElement(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 && _jsxs("div", __assign({
          className: prefixCls + "-filter-list-divider"
        }, {
          children: ["\u4E14", _jsx("span", {}, void 0)]
        }), void 0)]
      }, andCount);
    })
  }), void 0);
};

export default FilterList;

