import { _ as _asyncToGenerator, r as regenerator } from '../../index-qSfOyGhk.js';
import React__default from 'react';
import { render, screen, act, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { S as SelectParent, a as Select, O as Option } from '../../select-menu-CMlOgi2H.js';
import { separator } from '../index.js';
import '../../typeof-CgC7Bq6K.js';
import '../../slicedToArray-BB-AwwkK.js';
import '../../extends-C4cduGrv.js';
import '../../defineProperty-CEd-kZQW.js';
import '../../objectWithoutProperties-TXwlfNxG.js';
import 'prop-types';
import '../../index-CMfVNfTW.js';
import '../../toConsumableArray-dKQYsfpV.js';
import 'react-dom';
import 'classnames';
import '../../icon/assets/icon-map/index.js';
import '../../social-icon/index.js';
import '@fortawesome/react-fontawesome';
import '../../social-icon/icons.js';
import '../../social-icon/library.js';
import '@fortawesome/fontawesome-svg-core';
import '@fortawesome/free-solid-svg-icons';
import '@fortawesome/free-brands-svg-icons';
import '../../tooltip/index.js';
import '../../auto-id.esm-CjaDlg7r.js';
import '../../utils.esm-BKUEBdyY.js';
import '../../warning-s9zGJbnw.js';
import '../../portal.esm-D9LQ40g_.js';
import '@reach/visually-hidden';
import '../../portal/index.js';
import '../../use-rerender-once/index.js';
import '../../use-force-update/index.js';
import '../../button/button-group.js';
import '../../inject-css-BqVV5N0s.js';
import '../../_commonjsHelpers-_1FafXWI.js';
import '../../label/index.js';
import '../autocomplete.js';

describe("SelectMenu", function () {
  it("renders a trigger with a default placeholder 'Select'", function () {
    render( /*#__PURE__*/React__default.createElement(SelectParent, {
      options: [{
        label: "Option 1",
        value: "option1"
      }, {
        label: "Option 2",
        value: "option2"
      }, {
        label: "Option 3",
        value: "option3"
      }]
    }));
    var trigger = screen.getByRole("combobox");
    expect(trigger).toBeInTheDocument();
    expect(trigger).toHaveTextContent("Select");
  });
  it("allows to set a custom placeholder", function () {
    render( /*#__PURE__*/React__default.createElement(SelectParent, {
      options: [{
        label: "Option 1",
        value: "option1"
      }, {
        label: "Option 2",
        value: "option2"
      }, {
        label: "Option 3",
        value: "option3"
      }],
      placeholder: "Custom placeholder"
    }));
    var trigger = screen.getByRole("combobox");
    expect(trigger).toBeInTheDocument();
    expect(trigger).toHaveTextContent("Custom placeholder");
  });
  it("renders a list of options", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee() {
    var trigger, options;
    return regenerator.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            render( /*#__PURE__*/React__default.createElement(SelectParent, {
              options: [{
                label: "Option 1",
                value: "option1"
              }, {
                label: "Option 2",
                value: "option2"
              }, {
                label: "Option 3",
                value: "option3"
              }]
            }));
            trigger = screen.getByRole("combobox");
            _context.next = 4;
            return act(function () {
              return userEvent.click(trigger);
            });

          case 4:
            options = screen.getAllByRole("option");
            expect(trigger).toBeInTheDocument();
            expect(options).toHaveLength(3);

          case 7:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  })));
  it("allows to select an option", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee2() {
    var handleChange, trigger, option;
    return regenerator.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            handleChange = jest.fn();
            render( /*#__PURE__*/React__default.createElement(SelectParent, {
              options: [{
                label: "Option 1",
                value: "option1"
              }, {
                label: "Option 2",
                value: "option2"
              }, {
                label: "Option 3",
                value: "option3"
              }],
              onChange: handleChange
            }));
            trigger = screen.getByRole("combobox");
            _context2.next = 5;
            return act(function () {
              return userEvent.click(trigger);
            });

          case 5:
            option = screen.getByText("Option 2");
            _context2.next = 8;
            return act(function () {
              return userEvent.click(option);
            });

          case 8:
            expect(handleChange).toHaveBeenCalledWith({
              target: {
                label: "Option 2",
                value: "option2",
                name: undefined
              }
            });

          case 9:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  })));
  it("allows to set a default selected option", function () {
    render( /*#__PURE__*/React__default.createElement(SelectParent, {
      options: [{
        label: "Option 1",
        value: "option1"
      }, {
        label: "Option 2",
        value: "option2"
      }, {
        label: "Option 3",
        value: "option3"
      }],
      value: "option2"
    }));
    var trigger = screen.getByRole("combobox");
    expect(trigger).toHaveTextContent("Option 2");
  });
  it("renders a visible label when prop is passed", function () {
    render( /*#__PURE__*/React__default.createElement(SelectParent, {
      label: "Select an option",
      options: [{
        label: "Option 1",
        value: "option1"
      }, {
        label: "Option 2",
        value: "option2"
      }, {
        label: "Option 3",
        value: "option3"
      }]
    }));
    var label = screen.getByText("Select an option");
    expect(label).toBeInTheDocument();
  });
  it("applies aria-label to the reference when passed", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee3() {
    var trigger, listbox;
    return regenerator.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            render( /*#__PURE__*/React__default.createElement(SelectParent, {
              "aria-label": "Select an option",
              options: [{
                label: "Option 1",
                value: "option1"
              }, {
                label: "Option 2",
                value: "option2"
              }, {
                label: "Option 3",
                value: "option3"
              }]
            }));
            trigger = screen.getByRole("combobox");
            _context3.next = 4;
            return act(function () {
              return userEvent.click(trigger);
            });

          case 4:
            listbox = screen.getByRole("listbox");
            expect(trigger).toHaveAttribute("aria-label", "Select an option");
            expect(listbox).not.toHaveAttribute("aria-label", "Select an option");

          case 7:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  })));
  it("applies aria-labelledby to the reference when passed", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee4() {
    var trigger, listbox;
    return regenerator.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            render( /*#__PURE__*/React__default.createElement(SelectParent, {
              "aria-labelledby": "my-external-id",
              options: [{
                label: "Option 1",
                value: "option1"
              }, {
                label: "Option 2",
                value: "option2"
              }, {
                label: "Option 3",
                value: "option3"
              }]
            }));
            trigger = screen.getByRole("combobox");
            _context4.next = 4;
            return act(function () {
              return userEvent.click(trigger);
            });

          case 4:
            listbox = screen.getByRole("listbox");
            expect(trigger).toHaveAttribute("aria-labelledby", "my-external-id");
            expect(listbox).not.toHaveAttribute("aria-labelledby", "my-external-id");

          case 7:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  })));
  it("provides an event object with the selected option", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee5() {
    var handleChange, trigger, option;
    return regenerator.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            handleChange = jest.fn();
            render( /*#__PURE__*/React__default.createElement(SelectParent, {
              options: [{
                label: "Option 1",
                value: "option1"
              }, {
                label: "Option 2",
                value: "option2"
              }, {
                label: "Option 3",
                value: "option3"
              }],
              onChange: handleChange
            }));
            trigger = screen.getByRole("combobox");
            _context5.next = 5;
            return act(function () {
              return userEvent.click(trigger);
            });

          case 5:
            option = screen.getByRole("option", {
              name: /Option 2/i
            });
            _context5.next = 8;
            return act(function () {
              return userEvent.click(option);
            });

          case 8:
            expect(handleChange).toHaveBeenCalledWith({
              target: {
                label: "Option 2",
                value: "option2",
                name: undefined
              }
            });

          case 9:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5);
  })));
  it("renders a custom component as option", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee6() {
    var trigger, option;
    return regenerator.wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            render( /*#__PURE__*/React__default.createElement(SelectParent, {
              options: [{
                label: "Option 1",
                value: "option1"
              }, {
                label: "Option 2",
                value: "option2",
                component: /*#__PURE__*/React__default.createElement("div", null, "Custom Option")
              }, {
                label: "Option 3",
                value: "option3"
              }]
            }));
            trigger = screen.getByRole("combobox");
            _context6.next = 4;
            return act(function () {
              return userEvent.click(trigger);
            });

          case 4:
            option = screen.getByText("Custom Option");
            expect(option).toBeInTheDocument();

          case 6:
          case "end":
            return _context6.stop();
        }
      }
    }, _callee6);
  })));
  it("allows numbers as option values", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee7() {
    var handleChange, trigger, option;
    return regenerator.wrap(function _callee7$(_context7) {
      while (1) {
        switch (_context7.prev = _context7.next) {
          case 0:
            handleChange = jest.fn();
            render( /*#__PURE__*/React__default.createElement(SelectParent, {
              options: [{
                label: "Option 0",
                value: 0
              }, {
                label: "Option 1",
                value: 1
              }, {
                label: "Option 2",
                value: 2
              }, {
                label: "Option 3",
                value: 3
              }],
              onChange: handleChange
            }));
            trigger = screen.getByRole("combobox");
            _context7.next = 5;
            return act(function () {
              return userEvent.click(trigger);
            });

          case 5:
            option = screen.getByRole("option", {
              name: /Option 2/i
            });
            _context7.next = 8;
            return act(function () {
              return userEvent.click(option);
            });

          case 8:
            expect(handleChange).toHaveBeenCalledWith({
              target: {
                label: "Option 2",
                value: 2,
                name: undefined
              }
            });

          case 9:
          case "end":
            return _context7.stop();
        }
      }
    }, _callee7);
  })));
});
describe("Select", function () {
  it("uses a default placeholder 'Select'", function () {
    render( /*#__PURE__*/React__default.createElement(Select, null, /*#__PURE__*/React__default.createElement(Option, null, "Option 1"), /*#__PURE__*/React__default.createElement(Option, null, "Option 2"), /*#__PURE__*/React__default.createElement(Option, null, "Option 3")));
    var trigger = screen.getByRole("combobox");
    expect(trigger).toBeInTheDocument();
    expect(trigger).toHaveTextContent("Select");
  });
  it("provides index with selected option", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee8() {
    var handleChange, trigger, option;
    return regenerator.wrap(function _callee8$(_context8) {
      while (1) {
        switch (_context8.prev = _context8.next) {
          case 0:
            handleChange = jest.fn();
            render( /*#__PURE__*/React__default.createElement(Select, {
              onChange: handleChange
            }, /*#__PURE__*/React__default.createElement(Option, null, "Option 1"), /*#__PURE__*/React__default.createElement(Option, null, "Option 2"), /*#__PURE__*/React__default.createElement(Option, null, "Option 3")));
            trigger = screen.getByRole("combobox");
            _context8.next = 5;
            return act(function () {
              return userEvent.click(trigger);
            });

          case 5:
            option = screen.getByRole("option", {
              name: /Option 2/i
            });
            _context8.next = 8;
            return act(function () {
              return userEvent.click(option);
            });

          case 8:
            expect(handleChange).toHaveBeenCalledWith(1);

          case 9:
          case "end":
            return _context8.stop();
        }
      }
    }, _callee8);
  })));
  it("allows to set a custom placeholder", function () {
    render( /*#__PURE__*/React__default.createElement(Select, {
      placeholder: "Custom placeholder"
    }, /*#__PURE__*/React__default.createElement(Option, null, "Option 1"), /*#__PURE__*/React__default.createElement(Option, null, "Option 2"), /*#__PURE__*/React__default.createElement(Option, null, "Option 3")));
    var trigger = screen.getByRole("combobox");
    expect(trigger).toBeInTheDocument();
    expect(trigger).toHaveTextContent("Custom placeholder");
  });
  it("renders a list of options", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee9() {
    var trigger, options;
    return regenerator.wrap(function _callee9$(_context9) {
      while (1) {
        switch (_context9.prev = _context9.next) {
          case 0:
            render( /*#__PURE__*/React__default.createElement(Select, null, /*#__PURE__*/React__default.createElement(Option, null, "Option 1"), /*#__PURE__*/React__default.createElement(Option, null, "Option 2"), /*#__PURE__*/React__default.createElement(Option, null, "Option 3")));
            trigger = screen.getByRole("combobox");
            _context9.next = 4;
            return act(function () {
              return userEvent.click(trigger);
            });

          case 4:
            options = screen.getAllByRole("option");
            expect(trigger).toBeInTheDocument();
            expect(options).toHaveLength(3);

          case 7:
          case "end":
            return _context9.stop();
        }
      }
    }, _callee9);
  })));
  it("recognizes a selected option", function () {
    render( /*#__PURE__*/React__default.createElement(Select, null, /*#__PURE__*/React__default.createElement(Option, null, "Option 1"), /*#__PURE__*/React__default.createElement(Option, {
      selected: true
    }, "Option 2"), /*#__PURE__*/React__default.createElement(Option, null, "Option 3")));
    var trigger = screen.getByRole("combobox");
    expect(trigger).toHaveTextContent("Option 2");
  });
  it("reflects a changed option when there is an initial selected option", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee10() {
    var trigger, option;
    return regenerator.wrap(function _callee10$(_context10) {
      while (1) {
        switch (_context10.prev = _context10.next) {
          case 0:
            render( /*#__PURE__*/React__default.createElement(Select, null, /*#__PURE__*/React__default.createElement(Option, null, "Option 1"), /*#__PURE__*/React__default.createElement(Option, {
              selected: true
            }, "Option 2"), /*#__PURE__*/React__default.createElement(Option, null, "Option 3")));
            trigger = screen.getByRole("combobox");
            expect(trigger).toHaveTextContent("Option 2");
            _context10.next = 5;
            return act(function () {
              return userEvent.click(trigger);
            });

          case 5:
            option = screen.getByRole("option", {
              name: /Option 3/i
            });
            _context10.next = 8;
            return act(function () {
              return userEvent.click(option);
            });

          case 8:
            trigger = screen.getByRole("combobox");
            expect(trigger).toHaveTextContent("Option 3");

          case 10:
          case "end":
            return _context10.stop();
        }
      }
    }, _callee10);
  })));
  it("selects non-string children as option", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee11() {
    var options, trigger, renderedOptions;
    return regenerator.wrap(function _callee11$(_context11) {
      while (1) {
        switch (_context11.prev = _context11.next) {
          case 0:
            options = [{
              label: "Option 1",
              value: 1
            }, {
              label: "Option 2",
              value: 2
            }, {
              label: "Option 3",
              value: 3,
              component: /*#__PURE__*/React__default.createElement("div", null, "Custom Option")
            }];
            render( /*#__PURE__*/React__default.createElement(SelectParent, {
              options: options,
              value: 3
            }));
            trigger = screen.getByRole("combobox");
            expect(trigger).toHaveTextContent("Option 3");
            _context11.next = 6;
            return act(function () {
              return userEvent.click(trigger);
            });

          case 6:
            renderedOptions = screen.getAllByRole("option");
            expect(renderedOptions).toHaveLength(3);
            expect(renderedOptions[2]).toHaveTextContent("Custom Option");
            expect(renderedOptions[2]).toHaveAttribute("aria-selected", "true");

          case 10:
          case "end":
            return _context11.stop();
        }
      }
    }, _callee11);
  })));
  it("excludes separators as options", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee12() {
    var options, trigger, optionsList;
    return regenerator.wrap(function _callee12$(_context12) {
      while (1) {
        switch (_context12.prev = _context12.next) {
          case 0:
            options = [{
              label: "Option 1",
              value: 1
            }, separator, {
              label: "Option 2",
              value: 2
            }, separator, {
              label: "Option 3",
              value: 3
            }];
            render( /*#__PURE__*/React__default.createElement(SelectParent, {
              options: options
            }));
            trigger = screen.getByRole("combobox");
            _context12.next = 5;
            return act(function () {
              return userEvent.click(trigger);
            });

          case 5:
            optionsList = screen.getAllByRole("option");
            expect(optionsList).toHaveLength(3);

          case 7:
          case "end":
            return _context12.stop();
        }
      }
    }, _callee12);
  })));
  it("recalculates the label if the value is changed", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee14() {
    var options, _render, rerender;

    return regenerator.wrap(function _callee14$(_context14) {
      while (1) {
        switch (_context14.prev = _context14.next) {
          case 0:
            options = [{
              label: "Option 1",
              value: 1
            }, {
              label: "Option 2",
              value: 2
            }];
            _render = render( /*#__PURE__*/React__default.createElement(SelectParent, {
              options: options,
              value: 1
            })), rerender = _render.rerender;
            _context14.t0 = expect;
            _context14.next = 5;
            return screen.findByRole("combobox");

          case 5:
            _context14.t1 = _context14.sent;
            (0, _context14.t0)(_context14.t1).toHaveTextContent("Option 1");
            rerender( /*#__PURE__*/React__default.createElement(SelectParent, {
              options: options,
              value: 2
            }));
            _context14.next = 10;
            return waitFor( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee13() {
              return regenerator.wrap(function _callee13$(_context13) {
                while (1) {
                  switch (_context13.prev = _context13.next) {
                    case 0:
                      _context13.t0 = expect;
                      _context13.next = 3;
                      return screen.findByRole("combobox");

                    case 3:
                      _context13.t1 = _context13.sent;
                      (0, _context13.t0)(_context13.t1).toHaveTextContent("Option 2");

                    case 5:
                    case "end":
                      return _context13.stop();
                  }
                }
              }, _callee13);
            })));

          case 10:
          case "end":
            return _context14.stop();
        }
      }
    }, _callee14);
  })));
  it("uses typeahead to navigate through options", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee15() {
    var trigger;
    return regenerator.wrap(function _callee15$(_context15) {
      while (1) {
        switch (_context15.prev = _context15.next) {
          case 0:
            render( /*#__PURE__*/React__default.createElement(Select, null, /*#__PURE__*/React__default.createElement(Option, null, "Walleye"), /*#__PURE__*/React__default.createElement(Option, null, "Pike"), /*#__PURE__*/React__default.createElement(Option, null, "Perch"), /*#__PURE__*/React__default.createElement(Option, null, "Trout")));
            trigger = screen.getByRole("combobox");
            expect(trigger).toHaveTextContent("Select");
            _context15.next = 5;
            return act(function () {
              return userEvent.click(trigger);
            });

          case 5:
            _context15.next = 7;
            return act(function () {
              return userEvent.type(trigger, "e");
            });

          case 7:
            _context15.next = 9;
            return act(function () {
              return userEvent.type(trigger, "r");
            });

          case 9:
            _context15.next = 11;
            return act(function () {
              return userEvent.type(trigger, "c");
            });

          case 11:
            _context15.next = 13;
            return act(function () {
              return userEvent.keyboard("{Enter}");
            });

          case 13:
            expect(trigger).toHaveTextContent("Perch");

          case 14:
          case "end":
            return _context15.stop();
        }
      }
    }, _callee15);
  })));
  it("does not allow interaction when disabled", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee16() {
    var trigger, options;
    return regenerator.wrap(function _callee16$(_context16) {
      while (1) {
        switch (_context16.prev = _context16.next) {
          case 0:
            render( /*#__PURE__*/React__default.createElement(Select, {
              disabled: true
            }, /*#__PURE__*/React__default.createElement(Option, null, "Option 1"), /*#__PURE__*/React__default.createElement(Option, null, "Option 2"), /*#__PURE__*/React__default.createElement(Option, null, "Option 3")));
            trigger = screen.getByRole("combobox");
            expect(trigger).toHaveAttribute("aria-disabled", "true");
            expect(trigger).toHaveAttribute("aria-expanded", "false");
            _context16.next = 6;
            return act(function () {
              return userEvent.click(trigger);
            });

          case 6:
            options = screen.queryAllByRole("option");
            expect(options).toHaveLength(0);
            expect(trigger).toHaveAttribute("aria-expanded", "false");

          case 9:
          case "end":
            return _context16.stop();
        }
      }
    }, _callee16);
  })));
});
