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.object.to-string.js';
import React__default, { Fragment } from 'react';
import PropTypes from 'prop-types';
import { TableRow, TableCell } from '@carbon/react';
import classnames from 'classnames';
import { settings } from '../../../constants/Settings.js';

var iotPrefix = settings.iotPrefix;
var propTypes = {
  options: PropTypes.shape({
    hasRowExpansion: PropTypes.bool,
    hasRowSelection: PropTypes.oneOf(['multi', 'single', false]),
    // TODO: don't duplicate this one
    hasRowActions: PropTypes.bool,
    hasRowNesting: PropTypes.bool
  }),
  tableState: PropTypes.shape({
    aggregations: PropTypes.shape({
      label: PropTypes.string,
      columns: PropTypes.arrayOf(PropTypes.shape({
        /** id of the column that should have its values aggregated */
        id: PropTypes.string.isRequired,
        /** the value to be displayed */
        value: PropTypes.string,
        /** allow aligning the results the same as the column */
        align: PropTypes.oneOf(['start', 'center', 'end']),
        /** allows the aggregation to align with sortable columns extra padding */
        isSortable: PropTypes.bool
      }))
    }),
    /** TODO: Move this prop out and reuse in TableHead */
    /** What column ordering is currently applied to the table */
    ordering: PropTypes.arrayOf(PropTypes.shape({
      columnId: PropTypes.string.isRequired,
      isHidden: PropTypes.bool
    })).isRequired
  }).isRequired,
  testId: PropTypes.string,
  showExpanderColumn: PropTypes.bool
};
var defaultProps = {
  options: {},
  testId: 'table-foot',
  showExpanderColumn: false
};
var TableFoot = function TableFoot(_ref) {
  var testId = _ref.testId,
    _ref$options = _ref.options,
    hasRowExpansion = _ref$options.hasRowExpansion,
    hasRowSelection = _ref$options.hasRowSelection,
    hasRowActions = _ref$options.hasRowActions,
    hasRowNesting = _ref$options.hasRowNesting,
    _ref$tableState = _ref.tableState,
    aggregations = _ref$tableState.aggregations,
    ordering = _ref$tableState.ordering,
    showExpanderColumn = _ref.showExpanderColumn;
  var visibleColumns = ordering.filter(function (col) {
    return !col.isHidden;
  });
  var hasMultiSelect = hasRowSelection === 'multi';
  var hasExpandOrNest = hasRowExpansion || hasRowNesting;
  var labelColSpan = hasMultiSelect && hasExpandOrNest ? 3 : hasExpandOrNest || hasMultiSelect ? 2 : 1;
  return /*#__PURE__*/React__default.createElement("tfoot", {
    className: "".concat(iotPrefix, "-table-foot"),
    "data-testid": testId
  }, /*#__PURE__*/React__default.createElement(TableRow, {
    key: "aggregate-row"
  }, visibleColumns.map(function (orderedCol, index) {
    var aggregated = aggregations.columns.find(function (col) {
      return orderedCol.columnId === col.id;
    });
    var isLabelCell = !aggregated && index === 0;
    var cellTestId = "".concat(testId, "-aggregation-").concat(orderedCol.columnId);
    var cellKey = "".concat(orderedCol.columnId).concat(index);
    return isLabelCell ? /*#__PURE__*/React__default.createElement(TableCell, {
      className: "".concat(iotPrefix, "-table-foot--label"),
      "data-testid": cellTestId,
      key: cellKey,
      colSpan: labelColSpan
    }, aggregations.label) : aggregated ? /*#__PURE__*/React__default.createElement(Fragment, {
      key: "aggregated-cell-fragment-".concat(index)
    }, index === 0 && (hasMultiSelect || hasExpandOrNest) ? /*#__PURE__*/React__default.createElement(TableCell, {
      colSpan: hasMultiSelect && hasExpandOrNest ? 2 : 1
    }) : null, /*#__PURE__*/React__default.createElement(TableCell, {
      className: classnames(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "".concat(iotPrefix, "-table-foot--value"), true), 'data-table-end', aggregated.align === 'end'), 'data-table-start', !aggregated.align || aggregated.align === 'start'), 'data-table-center', aggregated.align === 'center'), "".concat(iotPrefix, "-table-foot--value__sortable"), aggregated.isSortable)),
      align: aggregated.align ? aggregated.align : undefined,
      "data-testid": cellTestId,
      key: cellKey
    }, aggregated.value)) : /*#__PURE__*/React__default.createElement(TableCell, {
      "data-testid": cellTestId,
      key: cellKey
    }, "\xA0");
  }), hasRowActions ? /*#__PURE__*/React__default.createElement(TableCell, null) : null, showExpanderColumn ? /*#__PURE__*/React__default.createElement(TableCell, {
    "data-testid": "".concat(testId, "-expander-column")
  }) : null));
};
TableFoot.propTypes = propTypes;
TableFoot.defaultProps = defaultProps;
TableFoot.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "TableFoot",
  "props": {
    "options": {
      "defaultValue": {
        "value": "{}",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "shape",
        "value": {
          "hasRowExpansion": {
            "name": "bool",
            "required": false
          },
          "hasRowSelection": {
            "name": "enum",
            "value": [{
              "value": "'multi'",
              "computed": false
            }, {
              "value": "'single'",
              "computed": false
            }, {
              "value": "false",
              "computed": false
            }],
            "required": false
          },
          "hasRowActions": {
            "name": "bool",
            "required": false
          },
          "hasRowNesting": {
            "name": "bool",
            "required": false
          }
        }
      },
      "required": false
    },
    "testId": {
      "defaultValue": {
        "value": "'table-foot'",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "showExpanderColumn": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "tableState": {
      "description": "",
      "type": {
        "name": "shape",
        "value": {
          "aggregations": {
            "name": "shape",
            "value": {
              "label": {
                "name": "string",
                "required": false
              },
              "columns": {
                "name": "arrayOf",
                "value": {
                  "name": "shape",
                  "value": {
                    "id": {
                      "name": "string",
                      "description": "id of the column that should have its values aggregated",
                      "required": true
                    },
                    "value": {
                      "name": "string",
                      "description": "the value to be displayed",
                      "required": false
                    },
                    "align": {
                      "name": "enum",
                      "value": [{
                        "value": "'start'",
                        "computed": false
                      }, {
                        "value": "'center'",
                        "computed": false
                      }, {
                        "value": "'end'",
                        "computed": false
                      }],
                      "description": "allow aligning the results the same as the column",
                      "required": false
                    },
                    "isSortable": {
                      "name": "bool",
                      "description": "allows the aggregation to align with sortable columns extra padding",
                      "required": false
                    }
                  }
                },
                "required": false
              }
            },
            "required": false
          },
          "ordering": {
            "name": "arrayOf",
            "value": {
              "name": "shape",
              "value": {
                "columnId": {
                  "name": "string",
                  "required": true
                },
                "isHidden": {
                  "name": "bool",
                  "required": false
                }
              }
            },
            "description": "What column ordering is currently applied to the table",
            "required": true
          }
        }
      },
      "required": true
    }
  }
};
var TableFoot$1 = TableFoot;

export { TableFoot$1 as default };
