import React, { useState } from 'react';
import classNames from 'classnames';
import { faFilter } from '@fortawesome/free-solid-svg-icons/faFilter';
import { faUndo } from '@fortawesome/free-solid-svg-icons/faUndo';
import { Button, ACCENT_TYPES } from '../button';
import { Modal } from '../modal';
import { useTranslation } from '../hooks/use-translation';
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
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. */
export function FilteringPanel({
  buttonTestId = 'filters-button',
  buttonText,
  titleText,
  clearFiltersText,
  applyFiltersText,
  closeOnClear = true,
  isValid = true,
  modalProps,
  isInitial,
  children,
  onApply,
  onClear,
  buttonProps = {
    onClick: () => {}
  }
}) {
  const {
    t
  } = useTranslation();
  const [isPanelOpen, setIsPanelOpen] = useState(false);
  const {
    onClick: buttonOnClick,
    ...otherButtonProps
  } = buttonProps;
  return /*#__PURE__*/_jsxs(_Fragment, {
    children: [/*#__PURE__*/_jsx(Button, {
      id: filteringPanelTriggerId,
      accentType: ACCENT_TYPES.COMMAND,
      leftIcon: faFilter,
      onClick: () => {
        if (buttonOnClick) {
          buttonOnClick();
        }
        setIsPanelOpen(true);
      },
      testId: buttonTestId,
      ...otherButtonProps,
      children: buttonText ?? t('Filters')
    }), isPanelOpen && /*#__PURE__*/_jsxs(Modal, {
      ...MODAL_DEFAULTS,
      ...modalProps,
      title: titleText ?? t('Filters'),
      contentClassName: classNames('filters-panel', modalProps.contentClassName),
      aria: ariaProp,
      onConfirmModal: () => {
        if (modalProps?.onConfirmModal) {
          modalProps.onConfirmModal();
        }
        setIsPanelOpen(false);
      },
      children: [/*#__PURE__*/_jsxs("div", {
        className: "filters-panel-controls",
        children: [/*#__PURE__*/_jsx(Button, {
          accentType: ACCENT_TYPES.PRIMARY,
          onClick: () => {
            setIsPanelOpen(false);
            onApply();
          },
          isDisabled: isInitial,
          testId: "filters-apply",
          children: applyFiltersText ?? t('Apply filters')
        }), /*#__PURE__*/_jsx(Button, {
          accentType: ACCENT_TYPES.SECONDARY,
          onClick: () => {
            if (closeOnClear) {
              setIsPanelOpen(false);
            }
            onClear();
          },
          leftIcon: faUndo,
          isDisabled: !isValid,
          testId: "filters-reset",
          children: clearFiltersText ?? t('Clear filters')
        })]
      }), /*#__PURE__*/_jsx("div", {
        className: "filters-panel-content",
        children: children
      })]
    })]
  });
}