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 } from "react/jsx-runtime";
import * as React from 'react';
import Icon from "../../../icon";
import { every, filter, flatten, get, isEmpty, isUndefined, map, max as getMax, min as getMin, pick, some } from 'lodash';
import classNames from 'classnames';
import isEqual from 'lodash/isEqual';
import isNull from 'lodash/isNull';
import { FieldType } from "../../enum";
import { DATE_NUM_KEYS } from "../../constants";
import { prefixCls } from "./Standard";
import { DateFilter, NumberFilter, TextFilter } from "../../../create-filter";
import { formatLabelValue } from "../tiled/TiledFieldItem";
var filterViewMap = {
  number: NumberFilter,
  datetime: DateFilter,
  text: TextFilter
};
/**
 * field,筛选字段，uuid， type， db, name,
 * filter,筛选条件，db，hType, objectType, list, min,max, start, end
 * onChange,每次点ok，就都抛出去
 * options(text: ['yyy', 'xxx'], number: [1,2], datetime: [2012-09-28, 2019-03-23])
 */

var StandardFieldItem = /*#__PURE__*/React.memo(function (_a) {
  var _b;

  var field = _a.field,
      fieldFilter = _a.fieldFilter,
      _c = _a.mode,
      mode = _c === void 0 ? 'multi' : _c,
      changeItem = _a.changeItem,
      onVisChange = _a.onVisibleChange,
      options = _a.options,
      textLabels = _a.textLabels;
  var type = field.type,
      isCombined = field.isCombined;
  var originOptionsRef = React.useRef(); //字段loading，这个针对于交叉筛选

  var _d = React.useState(false),
      valLoading = _d[0],
      setValLoading = _d[1];

  var isNum = type === FieldType.number;
  var isDatetime = type === FieldType.datetime;
  var isText = type === FieldType.text;
  React.useEffect(function () {
    //日期不可以交叉联动
    if (isDatetime) {
      setValLoading(false);
      return;
    }

    if (isEqual(options, originOptionsRef.current)) {
      setValLoading(!options);
    } else {
      originOptionsRef.current = options;
      setValLoading(false);
    }
  }, [isDatetime, options, valLoading]);
  var keys = DATE_NUM_KEYS[type];

  var _e = keys || [],
      fk = _e[0],
      sk = _e[1];

  var formatOptions = formatLabelValue(options, textLabels === null || textLabels === void 0 ? void 0 : textLabels.emptyValText);
  var cls = classNames(prefixCls + "-item", (_b = {}, _b[prefixCls + "-item-selected"] = isText ? !isEmpty(get(fieldFilter, 'list')) : fieldFilter && some(keys, function (k) {
    return fieldFilter[k] !== undefined && fieldFilter[k] !== null;
  }), _b));
  var onClick = React.useCallback(function () {
    setValLoading(true);
    onVisChange(field);
  }, [field, onVisChange]);
  var onOk = React.useCallback(function (val) {
    var _a;

    var _b;

    var value = {};

    if (isText) {
      value = {
        list: (_b = val === null || val === void 0 ? void 0 : val.value) !== null && _b !== void 0 ? _b : val
      };
    } else {
      //number和datetime 单选
      value = isCombined ? (_a = {}, _a[fk] = get(val, '[0][0]'), _a[sk] = get(val, '[0][1]'), _a) : val;
    }

    changeItem && changeItem(__assign(__assign({}, pick(field.detail, ['db', 'hType', 'hValue'])), value), field);
  }, [isText, changeItem, field, isCombined, fk, sk]); //被聚合

  var numIsCombine = isNum && isCombined;
  var dateIsCombine = isDatetime && isCombined;
  var displayTypeList = numIsCombine || dateIsCombine || isText;
  var viewType = displayTypeList ? 'text' : type;
  var Filter = filterViewMap[viewType];
  var commonProps = React.useMemo(function () {
    return {
      onOk: onOk,
      //暂定为数值，时间字段聚合后单选和选择模式为单选这两种情况
      radio: mode === 'single' || numIsCombine || dateIsCombine,
      clickAway: true,
      emptyValText: textLabels === null || textLabels === void 0 ? void 0 : textLabels.emptyVal,
      allSelectLabel: textLabels === null || textLabels === void 0 ? void 0 : textLabels.allCheck,
      loading: valLoading
    };
  }, [dateIsCombine, mode, numIsCombine, onOk, textLabels === null || textLabels === void 0 ? void 0 : textLabels.allCheck, textLabels === null || textLabels === void 0 ? void 0 : textLabels.emptyVal, valLoading]);
  var displayTypeListProps = React.useMemo(function () {
    var _a;

    var list = (_a = fieldFilter === null || fieldFilter === void 0 ? void 0 : fieldFilter.list) !== null && _a !== void 0 ? _a : [];
    return displayTypeList ? __assign(__assign({
      list: formatOptions
    }, commonProps), {
      values: !!formatOptions ? map(filter(formatOptions, function (it) {
        var _a;

        var flattenList = isText ? flatten(list) : [get(fieldFilter, "" + fk), get(fieldFilter, "" + sk)];
        return isText ? //这种情况是['a', 'b', 'c']
        typeof it === 'string' || isNull(it) ? flattenList.includes(it) : Array.isArray(it.value) ? //[{label: 'aa', value: ['a', 'b']}]
        every(it.value, function (i) {
          return flattenList.includes(i);
        }) : //[{label: 'aa', value: 'a'}]
        flattenList.includes(it.value) : isEqual((_a = it.value) !== null && _a !== void 0 ? _a : it, flattenList);
      }), function (it) {
        return !isUndefined(it.value) ? it.value : it;
      }) : []
    }) : {};
  }, [commonProps, displayTypeList, fieldFilter, fk, formatOptions, isText, sk]);
  var numberOrDateProps = React.useMemo(function () {
    var min = fieldFilter === null || fieldFilter === void 0 ? void 0 : fieldFilter.min;
    var max = fieldFilter === null || fieldFilter === void 0 ? void 0 : fieldFilter.max;
    var statisticMin = getMin(options);
    var statisticMax = getMax(options);
    return __assign(__assign({}, commonProps), {
      labelMax: textLabels === null || textLabels === void 0 ? void 0 : textLabels.labelMax,
      labelMin: textLabels === null || textLabels === void 0 ? void 0 : textLabels.labelMin,
      statisticMin: statisticMin,
      statisticMax: statisticMax,
      value: pick(fieldFilter, keys),
      max: max,
      min: min
    });
  }, [commonProps, fieldFilter, keys, options, textLabels === null || textLabels === void 0 ? void 0 : textLabels.labelMax, textLabels === null || textLabels === void 0 ? void 0 : textLabels.labelMin]);
  var filterProps = React.useMemo(function () {
    //filter 中的value有三种情况，就是text: list, number: {min, max},datetime: {fastType, start, end等}
    return displayTypeList ? displayTypeListProps : numberOrDateProps;
  }, [displayTypeList, displayTypeListProps, numberOrDateProps]);
  return _jsxs(React.Fragment, {
    children: [_jsx(Filter, __assign({}, filterProps, {
      children: _jsxs("div", __assign({
        className: cls,
        onClick: onClick
      }, {
        children: [_jsx("div", __assign({
          className: prefixCls + "-item-text"
        }, {
          children: field.name
        }), void 0), _jsx(Icon, {
          icon: "chevron-down",
          size: 12
        }, void 0)]
      }), void 0)
    }), void 0), _jsx("div", {
      className: prefixCls + "-item-split-line"
    }, void 0)]
  }, void 0);
});
export default StandardFieldItem;

