import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
import _createClass from '@babel/runtime/helpers/createClass';
import _possibleConstructorReturn from '@babel/runtime/helpers/possibleConstructorReturn';
import _getPrototypeOf from '@babel/runtime/helpers/getPrototypeOf';
import _inherits from '@babel/runtime/helpers/inherits';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import 'core-js/modules/es.array.concat.js';
import 'core-js/modules/es.array.filter.js';
import 'core-js/modules/es.array.find.js';
import 'core-js/modules/es.array.map.js';
import 'core-js/modules/es.array.splice.js';
import 'core-js/modules/es.function.name.js';
import 'core-js/modules/es.object.to-string.js';
import 'core-js/modules/es.reflect.construct.js';
import React__default, { Component } from 'react';
import PropTypes from 'prop-types';
import { TableRow, TableHeader, Button } from '@carbon/react';
import { settings } from '../../../../constants/Settings.js';
import { defaultFunction } from '../../../../utils/componentUtilityFunctions.js';
import { defaultI18NPropTypes } from '../../TablePropTypes.js';
import ColumnHeaderSelect from '../ColumnHeaderSelect/ColumnHeaderSelect.js';
import { DragAndDrop } from '../../../../utils/DragAndDropUtils.js';
import { Settings } from '@carbon/icons-react';

function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(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.iotPrefix;
var ColumnHeaderRow = /*#__PURE__*/function (_Component) {
  function ColumnHeaderRow() {
    var _this;
    _classCallCheck(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(_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(_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(ColumnHeaderRow, _Component);
  return _createClass(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.createElement(DragAndDrop, null, /*#__PURE__*/React__default.createElement(TableRow, {
        "data-testid": testId,
        className: "".concat(iotPrefix, "--column-header-row--table-row")
      }, hasDragAndDrop ? /*#__PURE__*/React__default.createElement(TableHeader, {
        className: "".concat(iotPrefix, "--column-header-row--table-header")
      }) : null, hasRowSelection === 'multi' ? /*#__PURE__*/React__default.createElement(TableHeader, {
        className: "".concat(iotPrefix, "--column-header-row--table-header")
      }) : null, hasRowExpansion ? /*#__PURE__*/React__default.createElement(TableHeader, {
        className: "".concat(iotPrefix, "--column-header-row--table-header")
      }) : null, /*#__PURE__*/React__default.createElement(TableHeader, {
        className: "".concat(iotPrefix, "--column-header-row--table-header"),
        colSpan: visibleColumns.length + (hasRowActions ? 1 : 0) + (showExpanderColumn ? 1 : 0),
        scope: "col"
      }, /*#__PURE__*/React__default.createElement("div", {
        className: "".concat(iotPrefix, "--column-header-row--select-wrapper")
      }, ordering.map(function (c, idx) {
        return /*#__PURE__*/React__default.createElement(ColumnHeaderSelect, {
          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.createElement(Button, {
        disabled: isDisabled,
        className: "column-header__btn",
        kind: "ghost",
        size: "sm",
        renderIcon: Settings,
        onClick: function onClick() {
          return onColumnSelectionConfig();
        }
      }, columnSelectionConfigText) : null)));
    }
  }]);
}(Component);
_defineProperty(ColumnHeaderRow, "propTypes", {
  columns: PropTypes.arrayOf(PropTypes.shape({
    id: PropTypes.string.isRequired,
    name: PropTypes.string.isRequired
  })).isRequired,
  ordering: PropTypes.arrayOf(PropTypes.shape({
    columnId: PropTypes.string.isRequired,
    /* Visibility of column in table, defaults to false */
    isHidden: PropTypes.bool
  })).isRequired,
  options: PropTypes.shape({
    hasRowSelection: PropTypes.oneOf(['multi', 'single', false]),
    hasRowExpansion: PropTypes.bool,
    hasColumnSelectionConfig: PropTypes.bool,
    hasRowActions: PropTypes.bool
  }).isRequired,
  onChangeOrdering: PropTypes.func.isRequired,
  onColumnToggle: PropTypes.func.isRequired,
  onColumnSelectionConfig: PropTypes.func,
  columnSelectionConfigText: PropTypes.string,
  isDisabled: PropTypes.bool,
  testId: PropTypes.string,
  /** shows an additional column that can expand/shrink as the table is resized  */
  showExpanderColumn: PropTypes.bool.isRequired,
  /** Set to true if the table support drag and drop. Inserts a cell in the "darg handle" column
   * for spacing. */
  hasDragAndDrop: PropTypes.bool
});
_defineProperty(ColumnHeaderRow, "defaultProps", {
  onColumnSelectionConfig: defaultFunction('actions.table.onColumnSelectionConfig'),
  columnSelectionConfigText: 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;

export { ColumnHeaderRow as UnconnectedColumnHeaderRow, ColumnHeaderRow$1 as default };
