"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _drawer = _interopRequireDefault(require("@alifd/next/lib/drawer"));
var _button = _interopRequireDefault(require("@alifd/next/lib/button"));
var _table = _interopRequireDefault(require("@alifd/next/lib/table"));
var _icon = _interopRequireDefault(require("@alifd/next/lib/icon"));
var _switch = _interopRequireDefault(require("@alifd/next/lib/switch"));
var _checkbox = _interopRequireDefault(require("@alifd/next/lib/checkbox"));
var _message = _interopRequireDefault(require("@alifd/next/lib/message"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _react = _interopRequireDefault(require("react"));
var _cloneDeep = _interopRequireDefault(require("lodash/cloneDeep"));
var _isEqual = _interopRequireDefault(require("lodash/isEqual"));
function getSelectedRowKeys(columns) {
  if (columns === void 0) {
    columns = [];
  }
  var selectedRowKeys = [];
  columns && columns.forEach(function (item) {
    var hidden = item.hidden,
      dataKey = item.dataKey;
    if (!hidden && dataKey) {
      selectedRowKeys.push(dataKey);
    }
  });
  return selectedRowKeys;
}
var WebCustomColumnDrawer = /*#__PURE__*/function (_React$Component) {
  (0, _inheritsLoose2["default"])(WebCustomColumnDrawer, _React$Component);
  function WebCustomColumnDrawer(props) {
    var _this;
    _this = _React$Component.call(this, props) || this;
    var columns = _this.props.columns;
    _this.state = {
      defaultColumns: (0, _cloneDeep["default"])(columns),
      currentColumns: (0, _cloneDeep["default"])(columns)
    };
    return _this;
  }
  var _proto = WebCustomColumnDrawer.prototype;
  _proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
    var defaultColumns = this.state.defaultColumns;
    var nextColumns = nextProps.columns;
    if (!(0, _isEqual["default"])(defaultColumns, nextColumns)) {
      this.setState({
        defaultColumns: (0, _cloneDeep["default"])(nextColumns),
        currentColumns: (0, _cloneDeep["default"])(nextColumns)
      });
    }
  };
  _proto.isHidden = function isHidden(dataKey) {
    var _this$state$currentCo = this.state.currentColumns,
      currentColumns = _this$state$currentCo === void 0 ? [] : _this$state$currentCo;
    var column = currentColumns.find(function (item) {
      return item.dataKey === dataKey;
    });
    return column ? !!column.hidden : false;
  };
  _proto.isLocked = function isLocked(dataKey) {
    var _this$state$currentCo2 = this.state.currentColumns,
      currentColumns = _this$state$currentCo2 === void 0 ? [] : _this$state$currentCo2;
    var column = currentColumns.find(function (item) {
      return item.dataKey === dataKey;
    });
    var lock = column && column.lock;
    if (lock === "none") {
      lock = undefined;
    }
    return !!lock;
  };
  _proto.isAllColumnsShown = function isAllColumnsShown() {
    var _this$state$currentCo3 = this.state.currentColumns,
      currentColumns = _this$state$currentCo3 === void 0 ? [] : _this$state$currentCo3;
    return !currentColumns.some(function (item) {
      return item.hidden;
    });
  };
  _proto.handleCheck = function handleCheck(checked, dataKey) {
    var _this$state$currentCo4 = this.state.currentColumns,
      currentColumns = _this$state$currentCo4 === void 0 ? [] : _this$state$currentCo4;
    var column = currentColumns.find(function (item) {
      return item.dataKey === dataKey;
    });
    if (column) {
      column.hidden = !checked;
      column.lock = false;
      this.forceUpdate();
    }
  };
  _proto.handleLock = function handleLock(checked, dataKey) {
    var _this$state$currentCo5 = this.state.currentColumns,
      currentColumns = _this$state$currentCo5 === void 0 ? [] : _this$state$currentCo5;
    var column = currentColumns.find(function (item) {
      return item.dataKey === dataKey;
    });
    if (column) {
      column.lock = checked ? 'left' : undefined;
      this.forceUpdate();
    }
  };
  _proto.restoreDefault = function restoreDefault() {
    var defaultColumns = this.state.defaultColumns;
    this.setState({
      currentColumns: (0, _cloneDeep["default"])(defaultColumns)
    });
  };
  _proto.handleOk = function handleOk() {
    var _this$state$currentCo6 = this.state.currentColumns,
      currentColumns = _this$state$currentCo6 === void 0 ? [] : _this$state$currentCo6;
    var _this$props = this.props,
      onOk = _this$props.onOk,
      _this$props$locale = _this$props.locale,
      locale = _this$props$locale === void 0 ? {} : _this$props$locale;
    var shownCount = 0;
    var fixedCount = 0;
    currentColumns.forEach(function (colums) {
      var lock = colums.lock,
        hidden = colums.hidden;
      if (!hidden) {
        shownCount++;
      }
      if (lock && lock != "none") {
        fixedCount++;
      }
    });
    if (currentColumns.length >= 2 && shownCount < 2) {
      _message["default"].error(locale.tipLeastColumns);
      return;
    }
    onOk && onOk(currentColumns);
  };
  _proto.handleCheckAll = function handleCheckAll(checked) {
    var _this$state$currentCo7 = this.state.currentColumns,
      currentColumns = _this$state$currentCo7 === void 0 ? [] : _this$state$currentCo7;
    currentColumns.forEach(function (item) {
      return item.hidden = !checked;
    });
    this.forceUpdate();
  };
  _proto.renderColumnShown = function renderColumnShown(value, record, hasCheckbox) {
    var _this2 = this;
    var nextTablePrefix = this.props.nextTablePrefix;
    var dataKey = record.dataKey;
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: nextTablePrefix + "cell-wrap"
    }, hasCheckbox ? /*#__PURE__*/_react["default"].createElement(_checkbox["default"], {
      className: nextTablePrefix + "cell-checkbox",
      onChange: function onChange(checked) {
        _this2.handleCheck(checked, dataKey);
      },
      checked: !this.isHidden(dataKey)
    }) : null, /*#__PURE__*/_react["default"].createElement("span", {
      className: nextTablePrefix + "cell"
    }, value));
  };
  _proto.renderColumnFixed = function renderColumnFixed(value, rowIndex, record) {
    var _this3 = this;
    var dataKey = record.dataKey;
    return this.isHidden(dataKey) ? null : /*#__PURE__*/_react["default"].createElement(_switch["default"], {
      checked: this.isLocked(dataKey),
      onChange: function onChange(checked) {
        _this3.handleLock(checked, dataKey);
      }
    });
  };
  _proto.render = function render() {
    var _this4 = this;
    var _this$props2 = this.props,
      nextTablePrefix = _this$props2.nextTablePrefix,
      onClose = _this$props2.onClose,
      visible = _this$props2.visible,
      _this$props2$locale = _this$props2.locale,
      locale = _this$props2$locale === void 0 ? {} : _this$props2$locale;
    var _this$state$currentCo8 = this.state.currentColumns,
      currentColumns = _this$state$currentCo8 === void 0 ? [] : _this$state$currentCo8;
    return /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_drawer["default"], {
      className: nextTablePrefix + "web-custom-column-drawer",
      width: "400px",
      height: "100%",
      visible: visible,
      placement: "right",
      closeable: "mask,esc",
      onClose: onClose
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: nextTablePrefix + "title"
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: nextTablePrefix + "title-txt"
    }, locale.customColumn), /*#__PURE__*/_react["default"].createElement(_icon["default"], {
      className: nextTablePrefix + "close",
      type: "close",
      size: "medium",
      onClick: onClose
    })), /*#__PURE__*/_react["default"].createElement("div", {
      className: nextTablePrefix + "center"
    }, /*#__PURE__*/_react["default"].createElement(_table["default"], {
      style: {
        margin: '12px'
      },
      dataSource: currentColumns.filter(function (item) {
        return !item.isGroup;
      }),
      hasBorder: false,
      primaryKey: "dataKey",
      rowSelection: {
        selectedRowKeys: getSelectedRowKeys(currentColumns),
        onSelectAll: function onSelectAll(selected) {
          _this4.handleCheckAll(selected);
        },
        onSelect: function onSelect(selected, records) {
          var dataKey = records.dataKey;
          _this4.handleCheck(selected, dataKey);
        }
      }
    }, /*#__PURE__*/_react["default"].createElement(_table["default"].Column, {
      cell: function cell(value, index, record) {
        return _this4.renderColumnShown(value, record, false);
      },
      title: locale.showColumn,
      dataIndex: "title",
      width: "60%"
    }), /*#__PURE__*/_react["default"].createElement(_table["default"].Column, {
      cell: this.renderColumnFixed.bind(this),
      title: locale.stickColumn,
      dataIndex: "lock",
      width: "40%"
    }))), /*#__PURE__*/_react["default"].createElement("div", {
      className: nextTablePrefix + "bottom"
    }, /*#__PURE__*/_react["default"].createElement(_button["default"], {
      className: nextTablePrefix + "button",
      onClick: onClose
    }, locale.cancel), /*#__PURE__*/_react["default"].createElement(_button["default"], {
      className: nextTablePrefix + "button",
      onClick: this.restoreDefault.bind(this)
    }, locale.restoreDefault), /*#__PURE__*/_react["default"].createElement(_button["default"], {
      style: {
        width: 80
      },
      type: "primary",
      onClick: this.handleOk.bind(this)
    }, locale.submit))));
  };
  return WebCustomColumnDrawer;
}(_react["default"].Component);
exports["default"] = WebCustomColumnDrawer;
WebCustomColumnDrawer.defaultProps = {
  columns: []
};