"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FilteringPanel = FilteringPanel;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _faFilter = require("@fortawesome/free-solid-svg-icons/faFilter");
var _faUndo = require("@fortawesome/free-solid-svg-icons/faUndo");
var _button = require("../button");
var _modal = require("../modal");
var _useTranslation = require("../hooks/use-translation");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const MODAL_DEFAULTS = {
  show: true,
  hideFooter: true,
  shouldCloseOnOverlayClick: true
};
const filteringPanelTriggerId = 'filtering-panel-trigger';
const ariaProp = {
  labelledby: filteringPanelTriggerId
};

/** Button which slides in a modal from the left for filtering selection. */
function FilteringPanel({
  buttonTestId = 'filters-button',
  buttonText,
  titleText,
  clearFiltersText,
  applyFiltersText,
  closeOnClear = true,
  isValid = true,
  modalProps,
  isInitial,
  children,
  onApply,
  onClear,
  buttonProps = {
    onClick: () => {}
  }
}) {
  const {
    t
  } = (0, _useTranslation.useTranslation)();
  const [isPanelOpen, setIsPanelOpen] = (0, _react.useState)(false);
  const {
    onClick: buttonOnClick,
    ...otherButtonProps
  } = buttonProps;
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_button.Button, {
      id: filteringPanelTriggerId,
      accentType: _button.ACCENT_TYPES.COMMAND,
      leftIcon: _faFilter.faFilter,
      onClick: () => {
        if (buttonOnClick) {
          buttonOnClick();
        }
        setIsPanelOpen(true);
      },
      testId: buttonTestId,
      ...otherButtonProps,
      children: buttonText ?? t('Filters')
    }), isPanelOpen && /*#__PURE__*/(0, _jsxRuntime.jsxs)(_modal.Modal, {
      ...MODAL_DEFAULTS,
      ...modalProps,
      title: titleText ?? t('Filters'),
      contentClassName: (0, _classnames.default)('filters-panel', modalProps.contentClassName),
      aria: ariaProp,
      onConfirmModal: () => {
        if (modalProps?.onConfirmModal) {
          modalProps.onConfirmModal();
        }
        setIsPanelOpen(false);
      },
      children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: "filters-panel-controls",
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_button.Button, {
          accentType: _button.ACCENT_TYPES.PRIMARY,
          onClick: () => {
            setIsPanelOpen(false);
            onApply();
          },
          isDisabled: isInitial,
          testId: "filters-apply",
          children: applyFiltersText ?? t('Apply filters')
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.Button, {
          accentType: _button.ACCENT_TYPES.SECONDARY,
          onClick: () => {
            if (closeOnClear) {
              setIsPanelOpen(false);
            }
            onClear();
          },
          leftIcon: _faUndo.faUndo,
          isDisabled: !isValid,
          testId: "filters-reset",
          children: clearFiltersText ?? t('Clear filters')
        })]
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "filters-panel-content",
        children: children
      })]
    })]
  });
}