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

import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import * as React from 'react';
import Icon from "../../../icon";
import { isEmpty, map } from 'lodash';
import useMeasure from 'react-use/lib/useMeasure';
import StandardFieldItem from "./StandardFieldItem";
import AnalysisBtn from "../AnalysisBtn";
import PackageDropDown from "./PackageDropDown";
import { getFieldFilter } from "../index";
export var prefixCls = "dmc-global-filter-sf";
var STANDARD_FILTER_HEIGHT = 38;
var DEFAULT_CONTAINER_STYLE = {
  height: STANDARD_FILTER_HEIGHT,
  overflow: 'hidden'
};
var EXPAN_CONTAINER_STYLE = {
  height: 'auto',
  overflow: 'auto'
};

var StandardFilter = function StandardFilter(_a) {
  var packages = _a.packages,
      currentPackage = _a.currentPackage,
      changeCurrentPackage = _a.changeCurrentPackage,
      startAnalysis = _a.startAnalysis,
      startAnalysisProps = _a.startAnalysisProps,
      list = _a.list,
      changeItem = _a.changeItem,
      suppFilter = _a.suppFilter,
      onVisibleChange = _a.onVisibleChange,
      fieldValueObj = _a.fieldValueObj,
      _b = _a.showPackage,
      showPackage = _b === void 0 ? true : _b,
      clearFilter = _a.clearFilter,
      textLabels = _a.textLabels;

  var _c = useMeasure(),
      filterWrapperRef = _c[0],
      filterHeight = _c[1].height;

  var _d = React.useState(false),
      expand = _d[0],
      setExpand = _d[1];

  var hasMore = filterHeight > STANDARD_FILTER_HEIGHT;
  var changeExpand = React.useCallback(function () {
    return setExpand(function (expand) {
      return !expand;
    });
  }, []);
  var style = React.useMemo(function () {
    return expand ? EXPAN_CONTAINER_STYLE : DEFAULT_CONTAINER_STYLE;
  }, [expand]);
  var mornIcon = React.useMemo(function () {
    return hasMore && _jsxs("div", __assign({
      className: prefixCls + "-item",
      onClick: changeExpand
    }, {
      children: [_jsx("div", __assign({
        className: prefixCls + "-item"
      }, {
        children: textLabels === null || textLabels === void 0 ? void 0 : textLabels.more
      }), void 0), _jsx(Icon, {
        icon: "chevron-" + (expand ? 'up' : 'down'),
        size: 12
      }, void 0)]
    }), void 0);
  }, [changeExpand, expand, hasMore, textLabels === null || textLabels === void 0 ? void 0 : textLabels.more]);
  var clearFilterNode = React.useMemo(function () {
    return showPackage && !isEmpty(currentPackage) && _jsxs(_Fragment, {
      children: [_jsx("div", {
        className: prefixCls + "-item-split-line"
      }, void 0), _jsx("div", __assign({
        className: prefixCls + "-more-icon",
        onClick: clearFilter
      }, {
        children: _jsx(Icon, {
          icon: "delete-2",
          size: 18
        }, void 0)
      }), void 0)]
    }, void 0);
  }, [clearFilter, currentPackage, showPackage]);
  return _jsxs("div", __assign({
    className: prefixCls + "-container"
  }, {
    children: [_jsx("div", __assign({
      className: prefixCls + "-all-wrapper"
    }, {
      children: _jsx("div", __assign({
        className: prefixCls + "-sub-wrapper"
      }, {
        children: _jsxs("div", __assign({
          className: prefixCls,
          style: style
        }, {
          children: [_jsx("div", __assign({
            className: prefixCls + "-content"
          }, {
            children: _jsxs("div", __assign({
              className: prefixCls + "-content-filter",
              ref: filterWrapperRef
            }, {
              children: [_jsx(PackageDropDown, {
                currentPackage: currentPackage,
                packages: packages,
                changeCurrentPackage: changeCurrentPackage
              }, void 0), map(list, function (field) {
                var filter = getFieldFilter(field.detail, suppFilter);
                return _jsx(StandardFieldItem, {
                  field: field,
                  fieldFilter: filter,
                  changeItem: changeItem,
                  onVisibleChange: onVisibleChange,
                  options: fieldValueObj[field.detail.uuid],
                  textLabels: textLabels
                }, field.detail.uuid);
              })]
            }), void 0)
          }), prefixCls + "-content"), _jsxs("div", __assign({
            className: prefixCls + "-action-icons"
          }, {
            children: [mornIcon, clearFilterNode]
          }), prefixCls + "-action-icons")]
        }), void 0)
      }), void 0)
    }), void 0), _jsx(AnalysisBtn, {
      className: prefixCls + "-analysis-button",
      startAnalysis: startAnalysis,
      startAnalysisProps: startAnalysisProps
    }, void 0)]
  }), void 0);
};

export default StandardFilter;

