'use strict';
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
import { useState, useEffect } from 'react';
import { isMacOs } from '../../meta/services/client-environment.js';
import { usePathname, useSearchParams } from '../../meta/client/framework/hooks.js';
import { resetAndCloseDialog, getSearchInputId, getSearchId } from './Search.funcs.js';
import { metaQueryParams } from '../../../fd-toolbox/constants/meta-query-params.js';
import { SearchDialog } from './search-dialog/SearchDialog.js';
import { useSearchDialog } from './search-dialog-provider.js';
import { SearchInput } from './search-input/SearchInput.js';
import { eventListenerEvents } from '../../../fd-toolbox/constants/events.js';
import { keyboardKeys } from '../../../fd-toolbox/constants/keyboard-keys.js';
import { useEmptyRef } from '../../../fd-toolbox/hooks/use-empty-ref.js';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
function Search({
  parentContextName,
  compactMode
}) {
  const searchDialog = useSearchDialog();
  const [searchQuery, setSearchQuery] = useState("");
  const [initialPathname, setInitialPathname] = useState("");
  const pathName = usePathname();
  const searchParams = useSearchParams();
  const searchInputRef = useEmptyRef();
  useEffect(() => {
    const handler = /* @__PURE__ */__name(e => {
      const isK = e.key?.toLowerCase() === keyboardKeys.k || e.code === keyboardKeys.kCode;
      if (isK && (isMacOs() && e.metaKey || !isMacOs() && e.ctrlKey) && searchDialog.setSearchDialogOpen) {
        e.preventDefault();
        const willOpen = !searchDialog.searchDialogOpen;
        searchDialog.setSearchDialogOpen(willOpen);
        if (willOpen) {
          searchInputRef.current?.focus();
        }
      }
    }, "handler");
    window.addEventListener(eventListenerEvents.keydown, handler);
    return () => window.removeEventListener(eventListenerEvents.keydown, handler);
  }, [searchDialog]);
  useEffect(() => {
    if (!searchParams.has(metaQueryParams.viewName) && !searchParams.has(metaQueryParams.id) && pathName !== initialPathname) {
      setInitialPathname(pathName);
      resetAndCloseDialog(setSearchQuery, searchDialog.setSearchDialogOpen);
    }
  }, [pathName, searchParams, initialPathname, searchDialog.setSearchDialogOpen]);
  return /* @__PURE__ */jsxs(Fragment, {
    children: [/* @__PURE__ */jsx(SearchInput, {
      parentContextName: getSearchInputId(parentContextName),
      searchQuery,
      onSearchQueryChanged: setSearchQuery,
      onOpenDialog: searchDialog.openDialog,
      className: "hidden md:flex",
      ref: searchInputRef
    }), searchDialog.searchDialogOpen && /* @__PURE__ */jsx(SearchDialog, {
      parentContextName: getSearchId(parentContextName),
      searchQuery,
      compactMode,
      onClose: () => resetAndCloseDialog(setSearchQuery, searchDialog.setSearchDialogOpen),
      onOpenDialog: searchDialog.openDialog,
      onSearchQueryChanged: setSearchQuery
    })]
  });
}
__name(Search, "Search");

export { Search };
