"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

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

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _react = _interopRequireWildcard(require("react"));

var _mobx = require("mobx");

var _mobxReactLite = require("mobx-react-lite");

var _map = _interopRequireDefault(require("lodash/map"));

var _noop = _interopRequireDefault(require("lodash/noop"));

var _omit = _interopRequireDefault(require("lodash/omit"));

var _localeContext = require("../../../locale-context");

var _select = _interopRequireDefault(require("../../../select"));

var _Record = _interopRequireDefault(require("../../../data-set/Record"));

var _enum = require("../../../data-set/enum");

var _singleton = require("../../../tooltip/singleton");

var _util = _interopRequireDefault(require("../../../overflow-tip/util"));

var _TableComboBar = require("../TableComboBar");

var _TableDynamicFilterBar = require("../TableDynamicFilterBar");

var _QuickFilterMenuContext = _interopRequireDefault(require("./QuickFilterMenuContext"));

/**
 * 快速筛选下拉
 */
var QuickFilterMenu = function QuickFilterMenu() {
  var _useContext = (0, _react.useContext)(_QuickFilterMenuContext["default"]),
      tempQueryFields = _useContext.tempQueryFields,
      autoQuery = _useContext.autoQuery,
      dataSet = _useContext.dataSet,
      menuDataSet = _useContext.menuDataSet,
      prefixCls = _useContext.prefixCls,
      queryDataSet = _useContext.queryDataSet,
      filterMenuDataSet = _useContext.filterMenuDataSet,
      conditionDataSet = _useContext.conditionDataSet,
      _useContext$onChange = _useContext.onChange,
      onChange = _useContext$onChange === void 0 ? _noop["default"] : _useContext$onChange,
      _useContext$onStatusC = _useContext.onStatusChange,
      onStatusChange = _useContext$onStatusC === void 0 ? _noop["default"] : _useContext$onStatusC,
      _useContext$onOrigina = _useContext.onOriginalChange,
      onOriginalChange = _useContext$onOrigina === void 0 ? _noop["default"] : _useContext$onOrigina,
      shouldLocateData = _useContext.shouldLocateData,
      initSearchId = _useContext.initSearchId,
      _useContext$filterCal = _useContext.filterCallback,
      filterCallback = _useContext$filterCal === void 0 ? _noop["default"] : _useContext$filterCal,
      _useContext$filterOpt = _useContext.filterOptionRenderer,
      filterOptionRenderer = _useContext$filterOpt === void 0 ? _noop["default"] : _useContext$filterOpt,
      tableStore = _useContext.tableStore;
  /**
   * queryDS 筛选赋值并更新初始勾选项
   * @param init
   */


  var conditionAssign = function conditionAssign(init) {
    onOriginalChange();
    var current = menuDataSet.current;
    var shouldQuery = false;

    if (current) {
      var conditionList = current.get('personalFilter') && (0, _TableDynamicFilterBar.parseValue)(current.get('personalFilter'));
      var initData = {};

      if (tempQueryFields) {
        (0, _mobx.runInAction)(function () {
          queryDataSet.fields = tempQueryFields;
        });
      }

      var currentQueryRecord = queryDataSet.current;

      if (conditionList && conditionList.length) {
        (0, _map["default"])(conditionList, function (condition) {
          var fieldName = condition.fieldName,
              value = condition.value;
          initData[fieldName] = (0, _TableDynamicFilterBar.parseValue)(value);
          onChange(fieldName);
        });
        onOriginalChange(Object.keys(initData));
        var emptyRecord = new _Record["default"]((0, _objectSpread2["default"])({}, initData), queryDataSet);
        dataSet.setState(_TableComboBar.SELECTFIELDS, Object.keys(initData));
        shouldQuery = !(0, _TableDynamicFilterBar.isEqualDynamicProps)(initData, currentQueryRecord ? (0, _omit["default"])(currentQueryRecord.toData(true), ['__dirty']) : {}, queryDataSet, currentQueryRecord);
        (0, _mobx.runInAction)(function () {
          queryDataSet.records.push(emptyRecord);
          queryDataSet.current = emptyRecord;
        });
        onStatusChange(_enum.RecordStatus.sync, emptyRecord.toData());
      } else {
        shouldQuery = !(0, _TableDynamicFilterBar.isEqualDynamicProps)(initData, currentQueryRecord ? (0, _omit["default"])(currentQueryRecord.toData(true), ['__dirty']) : {}, queryDataSet, currentQueryRecord);

        var _emptyRecord = new _Record["default"]({}, queryDataSet);

        dataSet.setState(_TableComboBar.SELECTFIELDS, []);
        (0, _mobx.runInAction)(function () {
          queryDataSet.records.push(_emptyRecord);
          queryDataSet.current = _emptyRecord;
        });
        onStatusChange(_enum.RecordStatus.sync);
      }

      var customizedColumn = current.get('personalColumn') && (0, _TableDynamicFilterBar.parseValue)(current.get('personalColumn'));

      if (tableStore) {
        (0, _mobx.runInAction)(function () {
          var newCustomized = {
            columns: (0, _objectSpread2["default"])({}, customizedColumn)
          };
          tableStore.tempCustomized = {
            columns: {}
          };
          tableStore.saveCustomized(newCustomized);
          tableStore.initColumns();
        });
      }

      if (!init && shouldQuery && autoQuery) {
        dataSet.query();
      }
    }
  };

  var handleQueryReset = function handleQueryReset() {
    if (filterMenuDataSet && filterMenuDataSet.current && filterMenuDataSet.current.get('filterName')) {
      // 筛选项重置重新赋值
      conditionAssign();
    } else {
      /**
       * 未选择或清除筛选项
       * 重置初始勾选项及初始赋值
       */
      queryDataSet.locate(0);
      var first = queryDataSet.get(0);

      if (first) {
        first.reset();
      }

      onOriginalChange();

      if (autoQuery) {
        dataSet.query();
      }
    }

    onStatusChange(_enum.RecordStatus.sync);
  };
  /**
   * 定位数据源
   * @param searchId
   * @param init 初始化
   */


  var locateData = function locateData(searchId, init) {
    var current = filterMenuDataSet.current;

    if (searchId) {
      filterCallback(searchId);
      menuDataSet.locate(menuDataSet.findIndex(function (menu) {
        return menu.get('searchId').toString() === searchId.toString();
      }));
      var menuRecord = menuDataSet.current;

      if (menuRecord) {
        var conditionList = menuRecord.get('personalFilter') && (0, _TableDynamicFilterBar.parseValue)(menuRecord.get('personalFilter'));
        conditionDataSet.loadData(conditionList);
      }

      if (current) {
        current.set('filterName', searchId);
      }

      conditionAssign(init);
    } else if (searchId === null) {
      handleQueryReset();
    } else if (menuDataSet.length) {
      menuDataSet.locate(0);
      var _menuRecord = menuDataSet.current;

      if (_menuRecord) {
        var _conditionList = _menuRecord.get('personalFilter') && (0, _TableDynamicFilterBar.parseValue)(_menuRecord.get('personalFilter'));

        conditionDataSet.loadData(_conditionList);

        if (current) {
          current.set('filterName', _menuRecord.get('searchId'));
        }

        var customizedColumn = _menuRecord.get('personalColumn') && (0, _TableDynamicFilterBar.parseValue)(_menuRecord.get('personalColumn'));

        if (tableStore) {
          (0, _mobx.runInAction)(function () {
            var newCustomized = {
              columns: (0, _objectSpread2["default"])({}, customizedColumn)
            };
            tableStore.tempCustomized = {
              columns: {}
            };
            tableStore.saveCustomized(newCustomized);
            tableStore.initColumns();
          });
        }
      }

      conditionAssign(init);
    } else if (current) {
      current.set('filterName', undefined);
    }
  };

  var handleChange = function handleChange(value) {
    var current = queryDataSet.current;

    if (current) {
      current.reset();
    }

    locateData(value);
  };

  (0, _react.useEffect)(function () {
    if (shouldLocateData) {
      locateData(initSearchId, true);
    }
  }, [shouldLocateData, initSearchId]);
  /**
   * 渲染下拉选项
   * @param record
   * @param text
   */

  var optionRenderer = function optionRenderer(_ref) {
    var value = _ref.value,
        text = _ref.text;
    var menuRecord = menuDataSet.find(function (menu) {
      return menu.get('searchId').toString() === value.toString();
    });
    var icon = menuRecord && menuRecord.get('searchIcon');
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(prefixCls, "-combo-filter-menu-option")
    }, /*#__PURE__*/_react["default"].createElement("span", {
      className: "".concat(prefixCls, "-combo-filter-menu-option-content"),
      onMouseEnter: function onMouseEnter(e) {
        var currentTarget = e.currentTarget;

        if ((0, _util["default"])(currentTarget)) {
          (0, _singleton.show)(currentTarget, {
            title: text
          });
        }
      },
      onMouseLeave: function onMouseLeave() {
        return (0, _singleton.hide)();
      }
    }, filterOptionRenderer(value, text, icon) || text));
  };

  return /*#__PURE__*/_react["default"].createElement(_select["default"], {
    isFlat: true,
    placeholder: (0, _localeContext.$l)('Table', 'fast_filter'),
    className: "".concat(prefixCls, "-combo-filterName-select"),
    popupCls: "".concat(prefixCls, "-combo-filterName-select-content"),
    dataSet: filterMenuDataSet,
    name: "filterName",
    dropdownMatchSelectWidth: false,
    dropdownMenuStyle: {
      width: '1.72rem'
    },
    optionRenderer: optionRenderer,
    onChange: handleChange,
    notFoundContent: (0, _localeContext.$l)('Table', 'no_save_filter'),
    clearButton: false,
    searchable: false
  });
};

QuickFilterMenu.displayName = 'QuickFilterMenu';

var _default = (0, _mobxReactLite.observer)(QuickFilterMenu);

exports["default"] = _default;
//# sourceMappingURL=QuickFilterMenu.js.map
