import _defineProperty from '@babel/runtime/helpers/defineProperty';
import isEqual from '../../../../node_modules/lodash-es/isEqual.js';
import uniqWith from '../../../../node_modules/lodash-es/uniqWith.js';
import 'core-js/modules/es.symbol.js';
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.slice.js';
import 'core-js/modules/es.function.name.js';
import 'core-js/modules/es.object.get-own-property-descriptor.js';
import 'core-js/modules/es.object.get-own-property-descriptors.js';
import 'core-js/modules/es.object.keys.js';
import 'core-js/modules/es.object.to-string.js';
import 'core-js/modules/web.dom-collections.for-each.js';
import React__default from 'react';
import PropTypes from 'prop-types';
import { TableRow } from '@carbon/react';
import classnames from 'classnames';
import { settings } from '../../../../constants/Settings.js';
import TableHeader from '../TableHeader.js';
import { TableColumnGroupPropType, TableOrderingPropType } from '../../TablePropTypes.js';

function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var iotPrefix = settings.iotPrefix;
var propTypes = {
  /** Number of additional columns that have been added to the end of the table */
  appendedColumns: PropTypes.number,
  /** Specify the properties of each column group in the table. Defaults to empty column. */
  columnGroups: TableColumnGroupPropType.isRequired,
  /** Specify the order, visibility and group belonging of the table columns */
  ordering: TableOrderingPropType.isRequired,
  /** Number of additional columns that have been added to the start of the table */
  prependedColumns: PropTypes.number,
  testId: PropTypes.string
};
var defaultProps = {
  appendedColumns: 0,
  prependedColumns: 0,
  testId: 'column-grouping-row'
};
var containsLastVisibleColumn = function containsLastVisibleColumn(groupId, visibleColumns) {
  var lastColumnInGroup = visibleColumns.filter(function (_ref) {
    var columnGroupId = _ref.columnGroupId;
    return columnGroupId === groupId;
  }).slice(-1)[0];
  var lastColumn = visibleColumns.slice(-1)[0];
  return lastColumnInGroup === lastColumn;
};
var ColumnGrouping = function ColumnGrouping(_ref2) {
  var appendedColumns = _ref2.appendedColumns,
    columnGroups = _ref2.columnGroups,
    ordering = _ref2.ordering,
    prependedColumns = _ref2.prependedColumns,
    testId = _ref2.testId;
  var visibleColumns = ordering.filter(function (col) {
    return !col.isHidden;
  });
  var groups = uniqWith(visibleColumns.filter(function (col) {
    return col.columnGroupId;
  }).map(function (col) {
    return _objectSpread(_objectSpread({}, columnGroups.find(function (colGroup) {
      return colGroup.id === col.columnGroupId;
    })), {}, {
      colspan: visibleColumns.filter(function (_ref3) {
        var columnGroupId = _ref3.columnGroupId;
        return columnGroupId === col.columnGroupId;
      }).length
    });
  }).filter(function (colGroupData) {
    return colGroupData.hasOwnProperty('id');
  }), isEqual);
  var isLastGroup = groups.slice(-1)[0];
  return /*#__PURE__*/React__default.createElement(TableRow, {
    "data-testid": testId,
    className: "".concat(iotPrefix, "--table-header__group-row")
  }, prependedColumns ? /*#__PURE__*/React__default.createElement(TableHeader, {
    key: "spacer-start",
    colSpan: prependedColumns,
    testId: "".concat(testId, "-prepended-column-group"),
    className: "".concat(iotPrefix, "--table-header__group-row-spacer")
  }) : null, groups.map(function (colGroupData) {
    return /*#__PURE__*/React__default.createElement(TableHeader, {
      title: colGroupData.name,
      key: "column-group-".concat(colGroupData.id),
      testId: "".concat(testId, "-column-group-").concat(colGroupData.id),
      className: classnames("".concat(iotPrefix, "--table-header__column-group"), _defineProperty({}, "".concat(iotPrefix, "--table-header__column-group--last-data-column"), colGroupData === isLastGroup && containsLastVisibleColumn(colGroupData.id, visibleColumns))),
      colSpan: colGroupData.colspan
    }, colGroupData.name);
  }), appendedColumns ? /*#__PURE__*/React__default.createElement(TableHeader, {
    key: "spacer-end",
    colSpan: appendedColumns,
    testId: "".concat(testId, "-appended-column-group"),
    className: "".concat(iotPrefix, "--table-header__group-row-spacer")
  }) : null);
};
ColumnGrouping.defaultProps = defaultProps;
ColumnGrouping.propTypes = propTypes;
ColumnGrouping.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "ColumnGrouping",
  "props": {
    "appendedColumns": {
      "defaultValue": {
        "value": "0",
        "computed": false
      },
      "description": "Number of additional columns that have been added to the end of the table",
      "type": {
        "name": "number"
      },
      "required": false
    },
    "prependedColumns": {
      "defaultValue": {
        "value": "0",
        "computed": false
      },
      "description": "Number of additional columns that have been added to the start of the table",
      "type": {
        "name": "number"
      },
      "required": false
    },
    "testId": {
      "defaultValue": {
        "value": "'column-grouping-row'",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "columnGroups": {
      "description": "Specify the properties of each column group in the table. Defaults to empty column.",
      "type": {
        "name": "custom",
        "raw": "TableColumnGroupPropType.isRequired"
      },
      "required": false
    },
    "ordering": {
      "description": "Specify the order, visibility and group belonging of the table columns",
      "type": {
        "name": "custom",
        "raw": "TableOrderingPropType.isRequired"
      },
      "required": false
    }
  }
};
var ColumnGrouping$1 = ColumnGrouping;

export { ColumnGrouping$1 as default };
