'use strict';

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

var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
var _createClass = require('@babel/runtime/helpers/createClass');
var _possibleConstructorReturn = require('@babel/runtime/helpers/possibleConstructorReturn');
var _getPrototypeOf = require('@babel/runtime/helpers/getPrototypeOf');
var _inherits = require('@babel/runtime/helpers/inherits');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
require('core-js/modules/es.array.concat.js');
require('core-js/modules/es.array.filter.js');
require('core-js/modules/es.array.find.js');
require('core-js/modules/es.array.map.js');
require('core-js/modules/es.array.splice.js');
require('core-js/modules/es.function.name.js');
require('core-js/modules/es.object.to-string.js');
require('core-js/modules/es.reflect.construct.js');
var React = require('react');
var PropTypes = require('prop-types');
var react = require('@carbon/react');
var Settings = require('../../../../constants/Settings.js');
var componentUtilityFunctions = require('../../../../utils/componentUtilityFunctions.js');
var TablePropTypes = require('../../TablePropTypes.js');
var ColumnHeaderSelect = require('../ColumnHeaderSelect/ColumnHeaderSelect.js');
var DragAndDropUtils = require('../../../../utils/DragAndDropUtils.js');
var iconsReact = require('@carbon/icons-react');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var _classCallCheck__default = /*#__PURE__*/_interopDefault(_classCallCheck);
var _createClass__default = /*#__PURE__*/_interopDefault(_createClass);
var _possibleConstructorReturn__default = /*#__PURE__*/_interopDefault(_possibleConstructorReturn);
var _getPrototypeOf__default = /*#__PURE__*/_interopDefault(_getPrototypeOf);
var _inherits__default = /*#__PURE__*/_interopDefault(_inherits);
var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);
var React__default = /*#__PURE__*/_interopDefault(React);
var PropTypes__default = /*#__PURE__*/_interopDefault(PropTypes);

function _callSuper(t, o, e) { return o = _getPrototypeOf__default.default(o), _possibleConstructorReturn__default.default(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf__default.default(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
var iotPrefix = Settings.settings.iotPrefix;
var ColumnHeaderRow = /*#__PURE__*/function (_Component) {
  function ColumnHeaderRow() {
    var _this;
    _classCallCheck__default.default(this, ColumnHeaderRow);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _callSuper(this, ColumnHeaderRow, [].concat(args));
    _defineProperty__default.default(_this, "reorderColumn", function (srcIndex, destIndex) {
      var _this$props = _this.props,
        ordering = _this$props.ordering,
        onChangeOrdering = _this$props.onChangeOrdering;
      ordering.splice(destIndex, 0, ordering.splice(srcIndex, 1)[0]);
      onChangeOrdering(ordering);
    });
    _defineProperty__default.default(_this, "toggleColumn", function (id) {
      var _this$props2 = _this.props,
        ordering = _this$props2.ordering,
        onColumnToggle = _this$props2.onColumnToggle;
      onColumnToggle(id, ordering.map(function (i) {
        return {
          columnId: i.columnId,
          isHidden: i.columnId === id ? !i.isHidden : i.isHidden
        };
      }));
    });
    return _this;
  }
  _inherits__default.default(ColumnHeaderRow, _Component);
  return _createClass__default.default(ColumnHeaderRow, [{
    key: "render",
    value: function render() {
      var _this2 = this;
      var _this$props3 = this.props,
        columns = _this$props3.columns,
        ordering = _this$props3.ordering,
        _this$props3$options = _this$props3.options,
        hasRowSelection = _this$props3$options.hasRowSelection,
        hasRowExpansion = _this$props3$options.hasRowExpansion,
        hasRowActions = _this$props3$options.hasRowActions,
        hasColumnSelectionConfig = _this$props3$options.hasColumnSelectionConfig,
        onColumnSelectionConfig = _this$props3.onColumnSelectionConfig,
        columnSelectionConfigText = _this$props3.columnSelectionConfigText,
        isDisabled = _this$props3.isDisabled,
        testId = _this$props3.testId,
        showExpanderColumn = _this$props3.showExpanderColumn,
        hasDragAndDrop = _this$props3.hasDragAndDrop;
      var visibleColumns = columns.filter(function (c) {
        return !(ordering.find(function (o) {
          return o.columnId === c.id;
        }) || {
          isHidden: false
        }).isHidden;
      });
      return /*#__PURE__*/React__default.default.createElement(DragAndDropUtils.DragAndDrop, null, /*#__PURE__*/React__default.default.createElement(react.TableRow, {
        "data-testid": testId,
        className: "".concat(iotPrefix, "--column-header-row--table-row")
      }, hasDragAndDrop ? /*#__PURE__*/React__default.default.createElement(react.TableHeader, {
        className: "".concat(iotPrefix, "--column-header-row--table-header")
      }) : null, hasRowSelection === 'multi' ? /*#__PURE__*/React__default.default.createElement(react.TableHeader, {
        className: "".concat(iotPrefix, "--column-header-row--table-header")
      }) : null, hasRowExpansion ? /*#__PURE__*/React__default.default.createElement(react.TableHeader, {
        className: "".concat(iotPrefix, "--column-header-row--table-header")
      }) : null, /*#__PURE__*/React__default.default.createElement(react.TableHeader, {
        className: "".concat(iotPrefix, "--column-header-row--table-header"),
        colSpan: visibleColumns.length + (hasRowActions ? 1 : 0) + (showExpanderColumn ? 1 : 0),
        scope: "col"
      }, /*#__PURE__*/React__default.default.createElement("div", {
        className: "".concat(iotPrefix, "--column-header-row--select-wrapper")
      }, ordering.map(function (c, idx) {
        return /*#__PURE__*/React__default.default.createElement(ColumnHeaderSelect.default, {
          key: "".concat(idx, "-item"),
          index: idx,
          columnId: c.columnId,
          isHidden: c.isHidden,
          moveItem: function moveItem(srcIndex, destIndex) {
            return _this2.reorderColumn(srcIndex, destIndex);
          },
          onClick: function onClick() {
            return _this2.toggleColumn(c.columnId);
          },
          isDisabled: isDisabled
        }, columns.find(function (i) {
          return c.columnId === i.id;
        }).name);
      })), hasColumnSelectionConfig ? /*#__PURE__*/React__default.default.createElement(react.Button, {
        disabled: isDisabled,
        className: "column-header__btn",
        kind: "ghost",
        size: "sm",
        renderIcon: iconsReact.Settings,
        onClick: function onClick() {
          return onColumnSelectionConfig();
        }
      }, columnSelectionConfigText) : null)));
    }
  }]);
}(React.Component);
_defineProperty__default.default(ColumnHeaderRow, "propTypes", {
  columns: PropTypes__default.default.arrayOf(PropTypes__default.default.shape({
    id: PropTypes__default.default.string.isRequired,
    name: PropTypes__default.default.string.isRequired
  })).isRequired,
  ordering: PropTypes__default.default.arrayOf(PropTypes__default.default.shape({
    columnId: PropTypes__default.default.string.isRequired,
    /* Visibility of column in table, defaults to false */
    isHidden: PropTypes__default.default.bool
  })).isRequired,
  options: PropTypes__default.default.shape({
    hasRowSelection: PropTypes__default.default.oneOf(['multi', 'single', false]),
    hasRowExpansion: PropTypes__default.default.bool,
    hasColumnSelectionConfig: PropTypes__default.default.bool,
    hasRowActions: PropTypes__default.default.bool
  }).isRequired,
  onChangeOrdering: PropTypes__default.default.func.isRequired,
  onColumnToggle: PropTypes__default.default.func.isRequired,
  onColumnSelectionConfig: PropTypes__default.default.func,
  columnSelectionConfigText: PropTypes__default.default.string,
  isDisabled: PropTypes__default.default.bool,
  testId: PropTypes__default.default.string,
  /** shows an additional column that can expand/shrink as the table is resized  */
  showExpanderColumn: PropTypes__default.default.bool.isRequired,
  /** Set to true if the table support drag and drop. Inserts a cell in the "darg handle" column
   * for spacing. */
  hasDragAndDrop: PropTypes__default.default.bool
});
_defineProperty__default.default(ColumnHeaderRow, "defaultProps", {
  onColumnSelectionConfig: componentUtilityFunctions.defaultFunction('actions.table.onColumnSelectionConfig'),
  columnSelectionConfigText: TablePropTypes.defaultI18NPropTypes.columnSelectionConfig,
  isDisabled: false,
  testId: '',
  hasDragAndDrop: false
});
ColumnHeaderRow.__docgenInfo = {
  "description": "",
  "methods": [{
    "name": "reorderColumn",
    "docblock": null,
    "modifiers": [],
    "params": [{
      "name": "srcIndex",
      "type": null
    }, {
      "name": "destIndex",
      "type": null
    }],
    "returns": null
  }, {
    "name": "toggleColumn",
    "docblock": null,
    "modifiers": [],
    "params": [{
      "name": "id",
      "type": null
    }],
    "returns": null
  }],
  "displayName": "ColumnHeaderRow",
  "props": {
    "onColumnSelectionConfig": {
      "defaultValue": {
        "value": "defaultFunction('actions.table.onColumnSelectionConfig')",
        "computed": true
      },
      "description": "",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "columnSelectionConfigText": {
      "defaultValue": {
        "value": "defaultI18NPropTypes.columnSelectionConfig",
        "computed": true
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "isDisabled": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "testId": {
      "defaultValue": {
        "value": "''",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "hasDragAndDrop": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "Set to true if the table support drag and drop. Inserts a cell in the \"darg handle\" column\nfor spacing.",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "columns": {
      "description": "",
      "type": {
        "name": "arrayOf",
        "value": {
          "name": "shape",
          "value": {
            "id": {
              "name": "string",
              "required": true
            },
            "name": {
              "name": "string",
              "required": true
            }
          }
        }
      },
      "required": true
    },
    "ordering": {
      "description": "",
      "type": {
        "name": "arrayOf",
        "value": {
          "name": "shape",
          "value": {
            "columnId": {
              "name": "string",
              "required": true
            },
            "isHidden": {
              "name": "bool",
              "required": false
            }
          }
        }
      },
      "required": true
    },
    "options": {
      "description": "",
      "type": {
        "name": "shape",
        "value": {
          "hasRowSelection": {
            "name": "enum",
            "value": [{
              "value": "'multi'",
              "computed": false
            }, {
              "value": "'single'",
              "computed": false
            }, {
              "value": "false",
              "computed": false
            }],
            "required": false
          },
          "hasRowExpansion": {
            "name": "bool",
            "required": false
          },
          "hasColumnSelectionConfig": {
            "name": "bool",
            "required": false
          },
          "hasRowActions": {
            "name": "bool",
            "required": false
          }
        }
      },
      "required": true
    },
    "onChangeOrdering": {
      "description": "",
      "type": {
        "name": "func"
      },
      "required": true
    },
    "onColumnToggle": {
      "description": "",
      "type": {
        "name": "func"
      },
      "required": true
    },
    "showExpanderColumn": {
      "description": "shows an additional column that can expand/shrink as the table is resized",
      "type": {
        "name": "bool"
      },
      "required": true
    }
  }
};
var ColumnHeaderRow$1 = ColumnHeaderRow;

exports.UnconnectedColumnHeaderRow = ColumnHeaderRow;
exports.default = ColumnHeaderRow$1;
