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

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

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

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

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

var react_use_1 = require("react-use");

var omit_1 = __importDefault(require("lodash/omit"));

var find_1 = __importDefault(require("lodash/find"));

var enum_1 = require("../enum");

var constants_1 = require("../constants");

var Tiled_1 = __importDefault(require("./tiled/Tiled"));

var Standard_1 = __importDefault(require("./standard/Standard"));

require("../style/filter.css"); //获取当前数据包当前字段的filter


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

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

exports.getFieldFilter = getFieldFilter;

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 = react_use_1.useMeasure(),
      domref = _d[0],
      childrenHeight = _d[1].height; //平铺筛选器的最大高度


  var HALF_CLIENT_HEIGHT = window.document.body.clientHeight / 2;
  var isTiled = filterType === enum_1.FilterTypeEnum.tiled;
  var cls = classnames_1["default"]("" + constants_1.prefix, "" + className, (_b = {}, _b[constants_1.prefix + "-s"] = !isTiled, _b[constants_1.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 - constants_1.TILED_FIELD_ROW_HEIGHT ? HALF_CLIENT_HEIGHT : 'auto'
    }, omit_1["default"](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_runtime_1.jsx("div", __assign({}, restProps, {
    className: cls,
    style: mixedStyle
  }, {
    children: isTiled ? jsx_runtime_1.jsx(Tiled_1["default"], __assign({}, tiledProps), void 0) : jsx_runtime_1.jsx(Standard_1["default"], __assign({}, standProps), void 0)
  }), void 0);
};

FilterView.defaultProps = {
  filterType: enum_1.FilterTypeEnum.standard,
  showPackage: true
};
exports["default"] = FilterView;

