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 __rest = this && this.__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;
};

import { jsx as _jsx } from "react/jsx-runtime";
import * as React from 'react';
import classNames from 'classnames';
import { useMeasure } from 'react-use';
import omit from 'lodash/omit';
import find from 'lodash/find';
import { FilterTypeEnum } from "../enum";
import { prefix, TILED_FIELD_ROW_HEIGHT } from "../constants";
import TiledFilter from "./tiled/Tiled";
import StandardFilter from "./standard/Standard";
import "../style/filter.css"; //获取当前数据包当前字段的filter

export var getFieldFilter = function getFieldFilter(field, suppFilter) {
  var _a, _b;

  var currentFilters = (_b = (_a = find(suppFilter, {
    packageUuid: field.sourcePackageUuid
  })) === null || _a === void 0 ? void 0 : _a.filters) !== null && _b !== void 0 ? _b : [];
  var filter = find(currentFilters, {
    hValue: field.hValue
  });
  return filter;
};

var FilterView = function FilterView(_a) {
  var _b;

  var filterType = _a.filterType,
      className = _a.className,
      style = _a.style,
      list = _a.list,
      onChange = _a.onChange,
      onClick = _a.startAnalysis,
      startAnalysisProps = _a.startAnalysisProps,
      showPackage = _a.showPackage,
      _c = _a.packageInfo,
      packages = _c.packages,
      currentPackage = _c.currentPackage,
      changeCurrentPackage = _c.changeCurrentPackage,
      onVisibleChange = _a.onVisibleChange,
      fieldValueObj = _a.fieldValueObj,
      fieldCount = _a.fieldCount,
      onOpen = _a.onOpen,
      suppFilter = _a.suppFilter,
      clearFilter = _a.clearFilter,
      textLabels = _a.textLabels,
      restProps = __rest(_a, ["filterType", "className", "style", "list", "onChange", "startAnalysis", "startAnalysisProps", "showPackage", "packageInfo", "onVisibleChange", "fieldValueObj", "fieldCount", "onOpen", "suppFilter", "clearFilter", "textLabels"]);

  var _d = useMeasure(),
      domref = _d[0],
      childrenHeight = _d[1].height; //平铺筛选器的最大高度


  var HALF_CLIENT_HEIGHT = window.document.body.clientHeight / 2;
  var isTiled = filterType === FilterTypeEnum.tiled;
  var cls = classNames("" + prefix, "" + className, (_b = {}, _b[prefix + "-s"] = !isTiled, _b[prefix + "-t"] = isTiled, _b));
  var changeItem = React.useCallback(function (filterItem, field) {
    onChange && onChange(filterItem, field);
  }, [onChange]);
  var mixedStyle = React.useMemo(function () {
    return __assign({
      height: isTiled && childrenHeight > HALF_CLIENT_HEIGHT - TILED_FIELD_ROW_HEIGHT ? HALF_CLIENT_HEIGHT : 'auto'
    }, omit(style, 'height'));
  }, [HALF_CLIENT_HEIGHT, childrenHeight, isTiled, style]);
  var startAnalysis = React.useCallback(function () {
    onClick && onClick(suppFilter !== null && suppFilter !== void 0 ? suppFilter : []);
  }, [suppFilter, onClick]);
  var commProps = React.useMemo(function () {
    return {
      list: list,
      startAnalysis: startAnalysis,
      startAnalysisProps: startAnalysisProps,
      changeItem: changeItem,
      fieldValueObj: fieldValueObj,
      textLabels: textLabels,
      suppFilter: suppFilter,
      clearFilter: clearFilter
    };
  }, [list, clearFilter, suppFilter, startAnalysis, startAnalysisProps, changeItem, fieldValueObj, textLabels]);
  var tiledProps = React.useMemo(function () {
    return __assign(__assign({}, commProps), {
      onOpen: onOpen,
      ref: domref,
      fieldCount: fieldCount
    });
  }, [commProps, onOpen, domref, fieldCount]);
  var standProps = React.useMemo(function () {
    return __assign(__assign({}, commProps), {
      packages: packages,
      showPackage: showPackage,
      currentPackage: currentPackage,
      changeCurrentPackage: changeCurrentPackage,
      onVisibleChange: onVisibleChange
    });
  }, [commProps, packages, showPackage, currentPackage, changeCurrentPackage, onVisibleChange]);
  return _jsx("div", __assign({}, restProps, {
    className: cls,
    style: mixedStyle
  }, {
    children: isTiled ? _jsx(TiledFilter, __assign({}, tiledProps), void 0) : _jsx(StandardFilter, __assign({}, standProps), void 0)
  }), void 0);
};

FilterView.defaultProps = {
  filterType: FilterTypeEnum.standard,
  showPackage: true
};
export default FilterView;

