import { _ as _slicedToArray } from '../../slicedToArray-BB-AwwkK.js';
import { _ as _asyncToGenerator, r as regenerator } from '../../index-qSfOyGhk.js';
import React__default from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import MultiSelectFilter from '../filter.js';
import '../../typeof-CgC7Bq6K.js';
import '../../defineProperty-CEd-kZQW.js';
import '../../extends-C4cduGrv.js';
import '../../toConsumableArray-dKQYsfpV.js';
import '../../objectWithoutProperties-TXwlfNxG.js';
import '../../floating-ui.react-cWPyyUrN.js';
import 'react-dom';
import 'classnames';
import '../../select-menu-BvA3x4Nf.js';
import 'prop-types';
import '../../_commonjsHelpers-_1FafXWI.js';
import '../../label/index.js';
import '@reach/visually-hidden';
import '../../ck-icon/index.js';
import '../../ck-icon/assets/icon-map/index.js';
import '../../icon/index.js';
import '@fortawesome/react-fontawesome';
import '../../icon/icons.js';
import '../../icon/library.js';
import '@fortawesome/fontawesome-svg-core';
import '@fortawesome/pro-solid-svg-icons';
import '@fortawesome/pro-light-svg-icons';
import '@fortawesome/free-brands-svg-icons';
import '../../portal/index.js';
import '../../use-rerender-once/index.js';
import '../../use-force-update/index.js';
import '../input.js';
import '../../checkbox/index.js';

describe("MultiselectFilter", function () {
  var options = [{
    label: "Walleye",
    value: "walleye"
  }, {
    label: "Bass",
    value: "bass"
  }, {
    label: "Perch",
    value: "perch"
  }];
  it("applies aria-label to the reference when passed", function () {
    render( /*#__PURE__*/React__default.createElement(MultiSelectFilter, {
      "aria-label": "Select an option",
      options: [{
        label: "Option 1",
        value: "option1"
      }, {
        label: "Option 2",
        value: "option2"
      }, {
        label: "Option 3",
        value: "option3"
      }]
    }));
    var trigger = screen.getByRole("combobox");
    userEvent.click(trigger);
    var listbox = screen.getByRole("listbox");
    expect(trigger).toHaveAttribute("aria-label", "Select an option");
    expect(listbox).not.toHaveAttribute("aria-label", "Select an option");
  });
  it("applies aria-labelledby to the reference when passed", function () {
    render( /*#__PURE__*/React__default.createElement(MultiSelectFilter, {
      "aria-labelledby": "my-external-id",
      options: [{
        label: "Option 1",
        value: "option1"
      }, {
        label: "Option 2",
        value: "option2"
      }, {
        label: "Option 3",
        value: "option3"
      }]
    }));
    var trigger = screen.getByRole("combobox");
    userEvent.click(trigger);
    var listbox = screen.getByRole("listbox");
    expect(trigger).toHaveAttribute("aria-labelledby", "my-external-id");
    expect(listbox).not.toHaveAttribute("aria-labelledby", "my-external-id");
  });
  it("passes the selected options to the onChange callback", function () {
    var handleChange = jest.fn();
    render( /*#__PURE__*/React__default.createElement(MultiSelectFilter, {
      options: options,
      onChange: handleChange
    }));
    var combobox = screen.getByRole("combobox");
    userEvent.click(combobox);
    var listOptions = screen.getAllByRole("option");
    userEvent.click(listOptions[1]);
    userEvent.click(listOptions[2]);
    expect(handleChange).toHaveBeenCalledWith({
      target: {
        name: undefined,
        value: [options[1], options[2]]
      }
    });
  });
  it("shows an indicator when the filter is active", function () {
    render( /*#__PURE__*/React__default.createElement(MultiSelectFilter, {
      options: options
    }));
    var combobox = screen.getByRole("combobox");
    userEvent.click(combobox);
    var indicator = screen.getByRole("status");
    expect(indicator).toBeInTheDocument();
    expect(indicator).toHaveAccessibleName("0 filters active");
    var listOptions = screen.getAllByRole("option");
    userEvent.click(listOptions[1]);
    expect(indicator).toHaveAccessibleName("1 filter active");
    userEvent.click(listOptions[0]);
    expect(indicator).toHaveAccessibleName("2 filters active");
  });
  it("does not allow interaction with the filter when disabled", function () {
    render( /*#__PURE__*/React__default.createElement(MultiSelectFilter, {
      options: options,
      disabled: true
    }));
    var combobox = screen.getByRole("combobox");
    expect(combobox).toHaveAttribute("aria-disabled", "true");
    userEvent.click(combobox);
    var listOptions = screen.queryAllByRole("option");
    expect(listOptions).toHaveLength(0);
  });
  it("clears selection when value prop changes", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee() {
    var clearButton, combobox, listOptions, selectedOptions;
    return regenerator.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            render( /*#__PURE__*/React__default.createElement(ClearableFilter, null));
            clearButton = screen.getByTestId("clear-btn");
            combobox = screen.getByRole("combobox");
            userEvent.click(combobox);
            listOptions = screen.getAllByRole("option");
            expect(listOptions).toHaveLength(4);
            selectedOptions = screen.getAllByRole("option", {
              selected: true
            });
            expect(selectedOptions).toHaveLength(2);
            userEvent.click(clearButton);
            userEvent.click(combobox);
            listOptions = screen.getAllByRole("option");
            selectedOptions = screen.queryAllByRole("option", {
              selected: true
            });
            expect(listOptions).toHaveLength(4);
            expect(selectedOptions).toHaveLength(0);

          case 14:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  })));
});

function ClearableFilter() {
  var options = [{
    label: "Walleye",
    value: "walleye"
  }, {
    label: "Bass",
    value: "bass"
  }, {
    label: "Perch",
    value: "perch"
  }, {
    label: "Pike",
    value: "pike"
  }];

  var _React$useState = React__default.useState(["bass", "pike"]),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      value = _React$useState2[0],
      setValue = _React$useState2[1];

  var handleClick = function handleClick() {
    setValue([]);
  };

  return /*#__PURE__*/React__default.createElement("div", null, /*#__PURE__*/React__default.createElement(MultiSelectFilter, {
    options: options,
    value: value
  }), /*#__PURE__*/React__default.createElement("button", {
    "data-testid": "clear-btn",
    onClick: handleClick
  }, "Clear values"));
}
