"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 __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

Object.defineProperty(exports, "__esModule", {
  value: true
});

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

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

var icon_1 = __importDefault(require("../../../icon"));

var lodash_1 = require("lodash");

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

var isEqual_1 = __importDefault(require("lodash/isEqual"));

var isNull_1 = __importDefault(require("lodash/isNull"));

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

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

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

var create_filter_1 = require("../../../create-filter");

var TiledFieldItem_1 = require("../tiled/TiledFieldItem");

var filterViewMap = {
  number: create_filter_1.NumberFilter,
  datetime: create_filter_1.DateFilter,
  text: create_filter_1.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 = 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 === enum_1.FieldType.number;
  var isDatetime = type === enum_1.FieldType.datetime;
  var isText = type === enum_1.FieldType.text;
  React.useEffect(function () {
    //日期不可以交叉联动
    if (isDatetime) {
      setValLoading(false);
      return;
    }

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

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

  var formatOptions = TiledFieldItem_1.formatLabelValue(options, textLabels === null || textLabels === void 0 ? void 0 : textLabels.emptyValText);
  var cls = classnames_1["default"](Standard_1.prefixCls + "-item", (_b = {}, _b[Standard_1.prefixCls + "-item-selected"] = isText ? !lodash_1.isEmpty(lodash_1.get(fieldFilter, 'list')) : fieldFilter && lodash_1.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] = lodash_1.get(val, '[0][0]'), _a[sk] = lodash_1.get(val, '[0][1]'), _a) : val;
    }

    changeItem && changeItem(__assign(__assign({}, lodash_1.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 ? lodash_1.map(lodash_1.filter(formatOptions, function (it) {
        var _a;

        var flattenList = isText ? lodash_1.flatten(list) : [lodash_1.get(fieldFilter, "" + fk), lodash_1.get(fieldFilter, "" + sk)];
        return isText ? //这种情况是['a', 'b', 'c']
        typeof it === 'string' || isNull_1["default"](it) ? flattenList.includes(it) : Array.isArray(it.value) ? //[{label: 'aa', value: ['a', 'b']}]
        lodash_1.every(it.value, function (i) {
          return flattenList.includes(i);
        }) : //[{label: 'aa', value: 'a'}]
        flattenList.includes(it.value) : isEqual_1["default"]((_a = it.value) !== null && _a !== void 0 ? _a : it, flattenList);
      }), function (it) {
        return !lodash_1.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 = lodash_1.min(options);
    var statisticMax = lodash_1.max(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: lodash_1.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 jsx_runtime_1.jsxs(React.Fragment, {
    children: [jsx_runtime_1.jsx(Filter, __assign({}, filterProps, {
      children: jsx_runtime_1.jsxs("div", __assign({
        className: cls,
        onClick: onClick
      }, {
        children: [jsx_runtime_1.jsx("div", __assign({
          className: Standard_1.prefixCls + "-item-text"
        }, {
          children: field.name
        }), void 0), jsx_runtime_1.jsx(icon_1["default"], {
          icon: "chevron-down",
          size: 12
        }, void 0)]
      }), void 0)
    }), void 0), jsx_runtime_1.jsx("div", {
      className: Standard_1.prefixCls + "-item-split-line"
    }, void 0)]
  }, void 0);
});
exports["default"] = StandardFieldItem;

