import _extends from "@babel/runtime/helpers/extends";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import { __decorate } from "tslib";
import React, { Component } from 'react';
import classNames from 'classnames';
import { action, toJS } from 'mobx';
import isPromise from 'is-promise';
import noop from 'lodash/noop';
import KeyCode from '../../../es/_util/KeyCode';
import { math } from 'choerodon-ui/dataset';
import ConfigContext from '../../../es/config-provider/ConfigContext';
import Table from '../table/Table';
import TableProfessionalBar from '../table/query-bar/TableProfessionalBar';
import { SelectionMode, TableMode, TableQueryBarType } from '../table/enum';
import { DataSetEvents, DataSetSelection } from '../data-set/enum';
import autobind from '../_util/autobind';
import { getColumnKey } from '../table/utils';
import SelectionList, { SelectionsPosition } from './SelectionList';
import { TriggerViewMode } from '../trigger-field/TriggerField';
import Picture from '../picture';
import ObserverNumberField from '../number-field';

var LovView = /*#__PURE__*/function (_Component) {
  _inherits(LovView, _Component);

  var _super = _createSuper(LovView);

  function LovView() {
    var _this;

    _classCallCheck(this, LovView);

    _this = _super.apply(this, arguments);
    _this.resizedColumns = new Map();
    return _this;
  }

  _createClass(LovView, [{
    key: "componentWillMount",
    value: function componentWillMount() {
      var _this$props = this.props,
          dataSet = _this$props.dataSet,
          selection = _this$props.dataSet.selection,
          multiple = _this$props.multiple,
          viewMode = _this$props.viewMode;
      this.selection = selection;
      dataSet.selection = multiple ? DataSetSelection.multiple : DataSetSelection.single;

      if (viewMode === TriggerViewMode.popup && multiple) {
        dataSet.addEventListener(DataSetEvents.batchSelect, this.handleSelect);
        dataSet.addEventListener(DataSetEvents.batchUnSelect, this.handleSelect);
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      var _this$props2 = this.props,
          dataSet = _this$props2.dataSet,
          multiple = _this$props2.multiple,
          viewMode = _this$props2.viewMode;
      dataSet.selection = this.selection;

      if (viewMode === TriggerViewMode.popup && multiple) {
        dataSet.removeEventListener(DataSetEvents.batchSelect, this.handleSelect);
        dataSet.removeEventListener(DataSetEvents.batchUnSelect, this.handleSelect);
      }
    }
  }, {
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(nextProps) {
      var viewMode = this.props.viewMode;

      if (viewMode === TriggerViewMode.popup && nextProps.popupHidden) {
        return false;
      }

      return true;
    }
    /* istanbul ignore next */

  }, {
    key: "getColumns",
    value: function getColumns() {
      var _this2 = this;

      var _this$props3 = this.props,
          lovItems = _this$props3.config.lovItems,
          tableProps = _this$props3.tableProps,
          viewMode = _this$props3.viewMode;
      return lovItems ? lovItems.filter(function (_ref) {
        var gridField = _ref.gridField;
        return gridField === 'Y';
      }).sort(function (_ref2, _ref3) {
        var seq1 = _ref2.gridFieldSequence;
        var seq2 = _ref3.gridFieldSequence;
        return seq1 - seq2;
      }).map(function (_ref4) {
        var display = _ref4.display,
            gridFieldName = _ref4.gridFieldName,
            gridFieldWidth = _ref4.gridFieldWidth,
            gridFieldAlign = _ref4.gridFieldAlign,
            gridFieldType = _ref4.gridFieldType;
        var column = {};

        if (tableProps && tableProps.columns) {
          column = tableProps.columns.find(function (c) {
            return c.name === gridFieldName;
          });
        } // 渲染 lov 中的 超链接 和 图片类型字段


        column = column || {};

        if (gridFieldType && gridFieldType.toLowerCase() === 'href') {
          column.renderer = function (_ref5) {
            var value = _ref5.value;
            return value ? /*#__PURE__*/React.createElement("a", {
              href: value,
              title: value,
              target: "_blank",
              rel: "noopener noreferrer"
            }, value) : undefined;
          };
        } else if (gridFieldType && gridFieldType.toLowerCase() === 'picture') {
          column.renderer = function (_ref6) {
            var value = _ref6.value;
            return value ? /*#__PURE__*/React.createElement(Picture, {
              src: value,
              objectFit: "contain",
              height: "inherit",
              block: false
            }) : undefined;
          };
        } else if (gridFieldType && gridFieldType.toLowerCase() === 'percent') {
          column.renderer = function (_ref7) {
            var value = _ref7.value;
            return value ? "".concat(math.multipliedBy(value, 100), "%") : value;
          };
        }

        return _objectSpread(_objectSpread({}, column), {}, {
          key: gridFieldName,
          header: display,
          name: gridFieldName,
          width: viewMode === TriggerViewMode.popup ? gridFieldName ? _this2.resizedColumns.get(gridFieldName) : undefined : gridFieldWidth,
          align: gridFieldAlign,
          editor: false
        });
      }) : undefined;
    }
  }, {
    key: "closeModal",
    value: function closeModal(record) {
      if (record) {
        var _this$props4 = this.props,
            onSelect = _this$props4.onSelect,
            modal = _this$props4.modal;

        if (modal) {
          modal.close();
        }

        onSelect(record);
      }
    }
  }, {
    key: "handleSelect",
    value: function handleSelect(event) {
      var _this3 = this;

      var selectionMode = this.selectionMode;
      var _this$props5 = this.props,
          _this$props5$onBefore = _this$props5.onBeforeSelect,
          onBeforeSelect = _this$props5$onBefore === void 0 ? noop : _this$props5$onBefore,
          multiple = _this$props5.multiple,
          dataSet = _this$props5.dataSet,
          tableProps = _this$props5.tableProps;
      var records = selectionMode === SelectionMode.treebox ? dataSet.treeSelected : selectionMode === SelectionMode.rowbox || multiple ? dataSet.selected : dataSet.current ? [dataSet.current] : []; // 满足单选模式下，双击和勾选框选中均支持

      if (tableProps && tableProps.alwaysShowRowBox && !event) {
        records = dataSet.selected;
      }

      var record = multiple ? records : records[0];
      var beforeSelect = onBeforeSelect(record);

      if (isPromise(beforeSelect)) {
        return beforeSelect.then(function (result) {
          if (result !== false) {
            _this3.closeModal(record);
          }
        });
      }

      if (beforeSelect !== false) {
        this.closeModal(record);
      }

      return false;
    }
    /* istanbul ignore next */

  }, {
    key: "handleKeyDown",
    value: function handleKeyDown(e) {
      if (e.keyCode === KeyCode.ENTER) {
        this.handleSelect();
      }
    }
    /**
     * 单选 onRow 处理
     * @param props
     */

  }, {
    key: "handleRow",
    value: function handleRow(props) {
      var tableProps = this.props.tableProps;

      if (tableProps) {
        var onRow = tableProps.onRow;

        if (onRow) {
          return _objectSpread({
            onDoubleClick: this.handleSelect
          }, onRow(props));
        }
      }

      return {
        onDoubleClick: this.handleSelect
      };
    }
  }, {
    key: "handleSingleRow",
    value: function handleSingleRow(props) {
      var tableProps = this.props.tableProps;

      if (tableProps) {
        var onRow = tableProps.onRow;

        if (onRow) {
          return _objectSpread({
            onClick: this.handleSelect
          }, onRow(props));
        }
      }

      return {
        onClick: this.handleSelect
      };
    }
  }, {
    key: "handleColumnResize",
    value: function handleColumnResize(props) {
      var width = props.width,
          column = props.column;
      this.resizedColumns.set(getColumnKey(column), width);
    }
  }, {
    key: "renderTable",
    value: function renderTable() {
      var _this$props6 = this.props,
          dataSet = _this$props6.dataSet,
          _this$props6$config = _this$props6.config,
          queryBar = _this$props6$config.queryBar,
          height = _this$props6$config.height,
          treeFlag = _this$props6$config.treeFlag,
          queryColumns = _this$props6$config.queryColumns,
          _this$props6$config$t = _this$props6$config.tableProps,
          configTableProps = _this$props6$config$t === void 0 ? {} : _this$props6$config$t,
          lovItems = _this$props6$config.lovItems,
          multiple = _this$props6.multiple,
          tableProps = _this$props6.tableProps,
          viewMode = _this$props6.viewMode,
          context = _this$props6.context,
          showSelectedInView = _this$props6.showSelectedInView;
      var getConfig = context.getConfig;
      var columns = this.getColumns();
      var popup = viewMode === TriggerViewMode.popup;
      var modal = viewMode === TriggerViewMode.modal;
      var drawer = viewMode === TriggerViewMode.drawer;
      var percentItems = {};

      if (lovItems) {
        lovItems.filter(function (x) {
          return x.gridFieldType && x.gridFieldType.toLowerCase() === 'percent' && x.conditionField === 'Y';
        }).forEach(function (x) {
          percentItems[x.gridFieldName] = /*#__PURE__*/React.createElement(ObserverNumberField, {
            suffix: /*#__PURE__*/React.createElement("span", null, "%")
          });
        });
      }

      var lovTableProps = _objectSpread(_objectSpread(_objectSpread({
        autoFocus: true,
        mode: treeFlag === 'Y' ? TableMode.tree : TableMode.list,
        onKeyDown: this.handleKeyDown,
        dataSet: dataSet,
        columns: columns,
        queryFieldsLimit: queryColumns,
        queryBar: queryBar || getConfig('lovQueryBar') || getConfig('queryBar'),
        selectionMode: SelectionMode.none
      }, configTableProps), tableProps), {}, {
        queryFields: _objectSpread(_objectSpread({}, tableProps && tableProps.queryFields), percentItems),
        className: classNames(configTableProps && configTableProps.className, tableProps && tableProps.className),
        style: _objectSpread(_objectSpread({}, configTableProps && configTableProps.style), {}, {
          height: height
        }, tableProps && tableProps.style),
        queryBarProps: _objectSpread(_objectSpread(_objectSpread({}, tableProps && tableProps.queryBarProps), configTableProps && configTableProps.queryBarProps), getConfig('lovQueryBarProps'))
      });

      if (multiple) {
        if (popup || !tableProps || !tableProps.selectionMode) {
          if (lovTableProps.mode === TableMode.tree) {
            lovTableProps.selectionMode = SelectionMode.treebox;
          } else {
            lovTableProps.selectionMode = SelectionMode.rowbox;
          }
        }

        if (lovTableProps.selectionMode !== SelectionMode.rowbox && lovTableProps.selectionMode !== SelectionMode.treebox) {
          lovTableProps.highLightRow = false;
          lovTableProps.selectedHighLightRow = true;
        }
      } else if (popup) {
        lovTableProps.onRow = this.handleSingleRow;
      } else if (lovTableProps.selectionMode !== SelectionMode.rowbox) {
        lovTableProps.onRow = this.handleRow;
      } else {
        lovTableProps.highLightRow = false;
        lovTableProps.selectedHighLightRow = true;
      }

      if (popup) {
        if (lovTableProps.showSelectionCachedButton === undefined) {
          lovTableProps.showSelectionCachedButton = false;
          lovTableProps.showCachedSelection = true;
        }

        lovTableProps.autoFocus = false;
        lovTableProps.autoWidth = 'autoWidth' in lovTableProps ? lovTableProps.autoWidth : true;
        lovTableProps.onColumnResize = this.handleColumnResize;
      }

      var isProfessionalBar = lovTableProps.queryBar === TableQueryBarType.professionalBar;

      if (!popup && !lovTableProps.queryBar && isProfessionalBar) {
        lovTableProps.queryBar = function (props) {
          return /*#__PURE__*/React.createElement(TableProfessionalBar, _extends({}, props));
        };
      }

      if ((modal || drawer) && showSelectedInView) {
        lovTableProps.showSelectionTips = false;
      }

      this.selectionMode = lovTableProps.selectionMode;
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Table, _extends({}, lovTableProps)), modal && this.renderSelectionList());
    }
  }, {
    key: "renderSelectionList",
    value: function renderSelectionList() {
      var _this$props7 = this.props,
          dataSet = _this$props7.dataSet,
          _this$props7$valueFie = _this$props7.valueField,
          valueField = _this$props7$valueFie === void 0 ? '' : _this$props7$valueFie,
          _this$props7$textFiel = _this$props7.textField,
          textField = _this$props7$textFiel === void 0 ? '' : _this$props7$textFiel,
          _this$props7$config = _this$props7.config,
          treeFlag = _this$props7$config.treeFlag,
          configTableProps = _this$props7$config.tableProps,
          tableProps = _this$props7.tableProps,
          multiple = _this$props7.multiple,
          viewMode = _this$props7.viewMode,
          showSelectedInView = _this$props7.showSelectedInView,
          getSelectionProps = _this$props7.getSelectionProps,
          context = _this$props7.context;

      if (!showSelectedInView || !multiple) {
        return null;
      }

      if (!this.selectionMode) {
        var selectionMode = tableProps && tableProps.selectionMode || configTableProps && configTableProps.selectionMode;

        if (!selectionMode) {
          this.selectionMode = treeFlag === 'Y' ? SelectionMode.treebox : SelectionMode.rowbox;
        } else {
          this.selectionMode = selectionMode;
        }
      }

      var selectionsPosition = viewMode === TriggerViewMode.drawer ? SelectionsPosition.side : viewMode === TriggerViewMode.modal ? SelectionsPosition.below : undefined;
      return /*#__PURE__*/React.createElement(SelectionList, {
        dataSet: dataSet,
        treeFlag: treeFlag,
        valueField: valueField,
        textField: textField,
        selectionsPosition: selectionsPosition,
        selectionProps: getSelectionProps && getSelectionProps(),
        context: context
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props8 = this.props,
          modal = _this$props8.modal,
          viewRenderer = _this$props8.viewRenderer,
          dataSet = _this$props8.dataSet,
          viewMode = _this$props8.viewMode,
          lovConfig = _this$props8.config,
          textField = _this$props8.textField,
          valueField = _this$props8.valueField,
          multiple = _this$props8.multiple;

      if (modal) {
        modal.handleOk(this.handleSelect);
      }

      return /*#__PURE__*/React.createElement(React.Fragment, null, viewMode === TriggerViewMode.drawer && this.renderSelectionList(), /*#__PURE__*/React.createElement("div", null, viewRenderer ? toJS(viewRenderer({
        dataSet: dataSet,
        lovConfig: lovConfig,
        textField: textField,
        valueField: valueField,
        multiple: multiple,
        modal: modal
      })) : this.renderTable()));
    }
  }], [{
    key: "contextType",
    get: function get() {
      return ConfigContext;
    }
  }]);

  return LovView;
}(Component);

export { LovView as default };

__decorate([action], LovView.prototype, "componentWillMount", null);

__decorate([action], LovView.prototype, "componentWillUnmount", null);

__decorate([autobind], LovView.prototype, "handleSelect", null);

__decorate([autobind], LovView.prototype, "handleKeyDown", null);

__decorate([autobind], LovView.prototype, "handleRow", null);

__decorate([autobind], LovView.prototype, "handleSingleRow", null);

__decorate([autobind], LovView.prototype, "handleColumnResize", null);
//# sourceMappingURL=LovView.js.map
