"use strict";
var import_interactivity = require("@wordpress/interactivity");
const { actions } = (0, import_interactivity.store)(
  "core/search",
  {
    state: {
      get ariaLabel() {
        const {
          isSearchInputVisible,
          ariaLabelCollapsed,
          ariaLabelExpanded
        } = (0, import_interactivity.getContext)();
        return isSearchInputVisible ? ariaLabelExpanded : ariaLabelCollapsed;
      },
      get ariaControls() {
        const { isSearchInputVisible, inputId } = (0, import_interactivity.getContext)();
        return isSearchInputVisible ? null : inputId;
      },
      get type() {
        const { isSearchInputVisible } = (0, import_interactivity.getContext)();
        return isSearchInputVisible ? "submit" : "button";
      },
      get tabindex() {
        const { isSearchInputVisible } = (0, import_interactivity.getContext)();
        return isSearchInputVisible ? "0" : "-1";
      }
    },
    actions: {
      openSearchInput: (0, import_interactivity.withSyncEvent)((event) => {
        const ctx = (0, import_interactivity.getContext)();
        const { ref } = (0, import_interactivity.getElement)();
        if (!ctx.isSearchInputVisible) {
          event.preventDefault();
          ctx.isSearchInputVisible = true;
          ref.parentElement.querySelector("input").focus();
        }
      }),
      closeSearchInput() {
        const ctx = (0, import_interactivity.getContext)();
        ctx.isSearchInputVisible = false;
      },
      handleSearchKeydown: (0, import_interactivity.withSyncEvent)((event) => {
        const { ref } = (0, import_interactivity.getElement)();
        if (event?.key === "Escape") {
          actions.closeSearchInput();
          ref.querySelector("button").focus();
        }
      }),
      handleSearchFocusout: (0, import_interactivity.withSyncEvent)((event) => {
        const { ref } = (0, import_interactivity.getElement)();
        if (!ref.contains(event.relatedTarget) && event.target !== window.document.activeElement) {
          actions.closeSearchInput();
        }
      })
    }
  },
  { lock: true }
);
//# sourceMappingURL=view.js.map
