'use strict';

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
require('core-js/modules/es.symbol.js');
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.includes.js');
require('core-js/modules/es.array.map.js');
require('core-js/modules/es.object.get-own-property-descriptor.js');
require('core-js/modules/es.object.get-own-property-descriptors.js');
require('core-js/modules/es.object.keys.js');
require('core-js/modules/es.object.to-string.js');
require('core-js/modules/es.string.includes.js');
require('core-js/modules/web.dom-collections.for-each.js');
var React = require('react');
var Button = require('../../Button/Button.js');
var iconsReact = require('@carbon/icons-react');

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

var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);
var _toConsumableArray__default = /*#__PURE__*/_interopDefault(_toConsumableArray);
var React__default = /*#__PURE__*/_interopDefault(React);

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__default.default(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; }

/**
 * The useVisibilityToggle hook will append a "visibility toggle" button to each selected column
 * if the hasVisibilityToggle is true. This is done be defining custom row actions for the listitems
 * and since the custom row actions will replace all existing actions we must also add back the "remove" button.
 */
var useVisibilityToggle = function useVisibilityToggle(_ref) {
  var handleRemove = _ref.handleRemove,
    hasVisibilityToggle = _ref.hasVisibilityToggle,
    hiddenIds = _ref.hiddenIds,
    hideIconDescription = _ref.hideIconDescription,
    onChange = _ref.onChange,
    removeIconDescription = _ref.removeIconDescription,
    selectedColumnItems = _ref.selectedColumnItems,
    setHiddenIds = _ref.setHiddenIds,
    showIconDescription = _ref.showIconDescription,
    testId = _ref.testId;
  var toggleVisibility = React.useCallback(function (id, show) {
    setHiddenIds(function (prev) {
      var groupItem = selectedColumnItems.find(function (item) {
        return item.id === id && item.children;
      });
      var childIds = groupItem === null || groupItem === void 0 ? void 0 : groupItem.children.map(function (child) {
        return child.id;
      });
      return groupItem ? show ? prev.filter(function (prevId) {
        return !childIds.includes(prevId);
      }) : [].concat(_toConsumableArray__default.default(prev), _toConsumableArray__default.default(childIds)) : show ? prev.filter(function (prevId) {
        return prevId !== id;
      }) : [].concat(_toConsumableArray__default.default(prev), [id]);
    });
    onChange(show ? 'show' : 'hide', id);
  }, [selectedColumnItems, setHiddenIds, onChange]);
  var generateRowActions = React.useCallback(
  // We need to create custom row actions with icon buttons
  // that are state dependent.
  function (id, isHidden) {
    return function () {
      return [/*#__PURE__*/React__default.default.createElement(Button, {
        key: "".concat(id, "-list-item-visibility-button"),
        testId: "".concat(testId, "-toggle-visibility-button-").concat(id),
        renderIcon: isHidden ? iconsReact.ViewOff : iconsReact.View,
        hasIconOnly: true,
        kind: "ghost",
        size: "sm",
        onClick: function onClick() {
          return toggleVisibility(id, isHidden);
        },
        iconDescription: isHidden ? showIconDescription : hideIconDescription,
        title: isHidden ? showIconDescription : hideIconDescription
      }), /*#__PURE__*/React__default.default.createElement(Button, {
        key: "".concat(id, "-list-item-delete-button")
        // We use the "list-builder" test since we are "overwriting" the buttons
        // of the ListBuilder and want to have the same test-id for the same button
        // regardless of where it is rendered.
        ,
        testId: "".concat(testId, "-list-builder-remove-button-").concat(id),
        renderIcon: iconsReact.CloseOutline,
        hasIconOnly: true,
        kind: "ghost",
        size: "sm",
        onClick: function onClick() {
          return handleRemove(null, id);
        },
        iconDescription: removeIconDescription,
        title: removeIconDescription
      })];
    };
  }, [toggleVisibility, handleRemove, hideIconDescription, removeIconDescription, showIconDescription, testId]);
  var appendNewRowActions = React.useCallback(function (item) {
    var _item$children;
    var itemIsHidden = (_item$children = item.children) !== null && _item$children !== void 0 && _item$children.length ? item.children.every(function (child) {
      return hiddenIds.includes(child.id);
    }) : hiddenIds.includes(item.id);
    return _objectSpread(_objectSpread({}, item), {}, {
      content: _objectSpread(_objectSpread({}, item.content), {}, {
        rowActions: generateRowActions(item.id, itemIsHidden)
      }),
      children: item.children ? item.children.map(function (child) {
        return _objectSpread(_objectSpread({}, child), {}, {
          content: _objectSpread(_objectSpread({}, child.content), {}, {
            rowActions: generateRowActions(child.id, hiddenIds.includes(child.id))
          })
        });
      }) : undefined
    });
  }, [generateRowActions, hiddenIds]);
  return React.useMemo(function () {
    if (!hasVisibilityToggle) {
      return selectedColumnItems;
    }
    return selectedColumnItems.map(appendNewRowActions);
  }, [selectedColumnItems, hasVisibilityToggle, appendNewRowActions]);
};

exports.useVisibilityToggle = useVisibilityToggle;
