import 'core-js/modules/es.array.find.js';
import 'core-js/modules/es.array.join.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 { settings } from '../../constants/Settings.js';
import icons from '../../utils/bundledIcons.js';

var iotPrefix = settings.iotPrefix,
  prefix = settings.prefix;

/**
 * Use this function to traverse the tree structure of a set of table rows using Depth-first search (DFS)
 * and apply some function on each row. The function is applied once the recursion starts back-tracking.
 * @param rows The root node of your search space, an array of rows.
 * @param functionToApply Any function that should be applied on every row. Params are a row and an optional aggregatorObj.
 * @param aggregatorObj Used as a container to aggregate the result (e.g. a count or needle) if needed.
 */
var tableTraverser = function tableTraverser(rows, functionToApply, aggregatorObj) {
  rows.forEach(function (row) {
    if (row.children) {
      tableTraverser(row.children, functionToApply, aggregatorObj);
    }
    functionToApply(row, aggregatorObj);
  });
};
var findRow = function findRow(rowId, myRows) {
  var result = [];
  var applyFunc = function applyFunc(row, aggr) {
    if (row.id === rowId) {
      aggr.push(row);
    }
  };
  tableTraverser(myRows, applyFunc, result);
  return result[0];
};
var getRowAction = function getRowAction(data, actionId, rowId) {
  var item;
  for (var idx = 0; idx < data.length; idx += 1) {
    var element = data[idx];
    if (element.id === rowId) {
      item = element.rowActions.find(function (action) {
        return action.id === actionId;
      });
      if (item) {
        break;
      }
      if (Array.isArray(element === null || element === void 0 ? void 0 : element.children)) {
        item = getRowAction(element.children, actionId, rowId);
        if (item) {
          break;
        }
      }
    }
    if (Array.isArray(element === null || element === void 0 ? void 0 : element.children)) {
      item = getRowAction(element.children, actionId, rowId);
      if (item) {
        break;
      }
    }
  }
  return item;
};
var renderBundledIconUsingName = function renderBundledIconUsingName(iconName, label) {
  var Icon = icons[iconName];
  return /*#__PURE__*/React__default.createElement(Icon, {
    "aria-label": label
  });
};

/**
 * Helper method to render the itemText on OverflowMenuItems in the rowActions or
 * in the TableToolbar.
 *
 * @param {Object} obj The object containing the following props
 *     className the className to be applied to the wrapper div when an icon is render
 *     action The renderIcon and the labelText to create be used in the itemText prop
 *
 * @returns string|Element
 */
var renderTableOverflowItemText = function renderTableOverflowItemText(_ref) {
  var action = _ref.action,
    className = _ref.className;
  return action.renderIcon ? /*#__PURE__*/React__default.createElement("div", {
    className: className,
    title: action.labelText
  }, typeof action.renderIcon === 'string' ? renderBundledIconUsingName(action.renderIcon, action.labelText) : /*#__PURE__*/React__default.createElement(action.renderIcon, {
    description: action.labelText
  }), /*#__PURE__*/React__default.createElement("div", {
    className: "".concat(prefix, "--overflow-menu-options__option-content")
  }, action.labelText)) : action.labelText;
};

/**
 * Values for pinned column options
 */
var PIN_COLUMN = {
  NONE: 'none',
  FIRST: 'first',
  LAST: 'last'
};

/**
 * Returns string of class names for pinned column
 * @param {Object} object: an object containing:
 *  pinColumn position of column to pin
 *  hasRowSelection flag defining row selection option
 *  hasRowExpansion flag defining row expansion option
 *  hasRowNesting flag defining row nesting option
 * @returns {String} class names for pinned column
 */
var pinColumnClassNames = function pinColumnClassNames(_ref2) {
  var pinColumn = _ref2.pinColumn,
    hasRowSelection = _ref2.hasRowSelection,
    hasRowExpansion = _ref2.hasRowExpansion,
    hasRowNesting = _ref2.hasRowNesting;
  if (pinColumn === PIN_COLUMN.NONE) {
    return null;
  }
  if (pinColumn === PIN_COLUMN.LAST) {
    return "".concat(iotPrefix, "--pin-last-col");
  }
  var pinClassNames = [];
  pinClassNames.push("".concat(iotPrefix, "--pin-first-col"));
  if (hasRowExpansion || hasRowNesting) {
    pinClassNames.push("".concat(iotPrefix, "--pin-first-col--expand"));
  }
  if (hasRowSelection) {
    pinClassNames.push("".concat(iotPrefix, "--pin-first-col--select"));
  }
  return pinClassNames.join(' ');
};
renderBundledIconUsingName.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "renderBundledIconUsingName"
};
renderTableOverflowItemText.__docgenInfo = {
  "description": "Helper method to render the itemText on OverflowMenuItems in the rowActions or\nin the TableToolbar.\n\n@param {Object} obj The object containing the following props\n    className the className to be applied to the wrapper div when an icon is render\n    action The renderIcon and the labelText to create be used in the itemText prop\n\n@returns string|Element",
  "methods": [],
  "displayName": "renderTableOverflowItemText"
};

export { PIN_COLUMN, findRow, getRowAction, pinColumnClassNames, renderBundledIconUsingName, renderTableOverflowItemText, tableTraverser };
