"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _constants = require("../../constants");
var _Translate = _interopRequireDefault(require("../Translate"));
var _Button = _interopRequireDefault(require("../Button"));
var _Toolbar = _interopRequireWildcard(require("../Toolbar"));
var _Dropdown = _interopRequireDefault(require("../Dropdown"));
var _SegmentedControl = _interopRequireDefault(require("../SegmentedControl"));
var _Menu = _interopRequireDefault(require("../Menu"));
var _Checkbox = _interopRequireDefault(require("../Checkbox"));
var _Badge = _interopRequireDefault(require("../Badge"));
var _CardList = require("./CardList");
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

const renderSelection = (_ref, baseClassName) => {
  let {
    onSelectAll,
    onSelectNone,
    checked,
    badge,
    menuItems = []
  } = _ref;
  const hasDropdown = menuItems.length > 0;
  const buttonProps = {};
  const checkboxProps = {};
  if (hasDropdown) {
    buttonProps.caret = true;
    checkboxProps.onClick = e => e.stopPropagation();
    buttonProps.component = 'span';
  } else {
    buttonProps.component = 'label';
  }
  let button = /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
    ...buttonProps,
    className: `${baseClassName}--select-all-btn`,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Checkbox.default, {
      checked: checked,
      onChange: isChecked => {
        if (isChecked) {
          onSelectAll();
        } else {
          onSelectNone();
        }
      },
      ...checkboxProps
    })
  });
  if (badge) {
    if (typeof badge === 'string') {
      badge = {
        label: badge
      };
    }
    button = /*#__PURE__*/(0, _jsxRuntime.jsx)(_Badge.default, {
      ...badge,
      children: button
    });
  }
  if (hasDropdown) {
    button = /*#__PURE__*/(0, _jsxRuntime.jsx)(_Dropdown.default, {
      menu: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Menu.default, {
        children: menuItems
      }),
      children: button
    });
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Toolbar.ToolbarGroup, {
    title: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
      content: "CardList.toolbarSelect",
      fallback: "Select"
    }),
    children: button
  }, "selection");
};
const renderView = (onViewChange, currentView) => {
  const viewButtons = [{
    value: _CardList.VIEW_EXTENDED,
    title: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
      content: "CardList.viewCards",
      fallback: "Cards"
    }),
    icon: 'site-page'
  }, {
    value: _CardList.VIEW_TILE,
    title: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
      content: "CardList.viewTiles",
      fallback: "Tiles"
    }),
    icon: 'four-squares'
  }, {
    value: _CardList.VIEW_ROW,
    title: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
      content: "CardList.viewList",
      fallback: "List"
    }),
    icon: 'list'
  }];
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Toolbar.ToolbarGroup, {
    title: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
      content: "CardList.toolbarView",
      fallback: "View"
    }),
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_SegmentedControl.default, {
      buttons: viewButtons,
      selected: currentView,
      onChange: view => onViewChange(view)
    })
  }, "view");
};
const isToolbarExpander = element => /*#__PURE__*/(0, _react.isValidElement)(element) && element.type === _Toolbar.ToolbarExpander;
const CardListToolbar = _ref2 => {
  let {
    baseClassName,
    selection,
    onViewChange,
    currentView,
    groups
  } = _ref2;
  let toolbar = [];
  if (selection) {
    toolbar.push(renderSelection(selection, baseClassName));
  }
  toolbar = toolbar.concat(groups);
  if (onViewChange && currentView !== _CardList.VIEW_RESPONSIVE) {
    const hasExpander = groups.filter(isToolbarExpander).length > 0;
    if (!hasExpander) {
      toolbar.push( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Toolbar.ToolbarExpander, {}, "expander"));
    }
    toolbar.push(renderView(onViewChange, currentView));
  }
  if (toolbar.length === 0) {
    return null;
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Toolbar.default, {
    className: baseClassName,
    children: toolbar
  });
};
CardListToolbar.propTypes = {
  selection: _propTypes.default.object,
  groups: _propTypes.default.arrayOf(_propTypes.default.element).isRequired,
  onViewChange: _propTypes.default.func,
  currentView: _propTypes.default.string.isRequired,
  baseClassName: _propTypes.default.string
};
CardListToolbar.defaultProps = {
  selection: null,
  onViewChange: null,
  baseClassName: `${_constants.CLS_PREFIX}card-list__toolbar`
};
var _default = exports.default = CardListToolbar;