// src/utilities/autocomplete-config.ts
function setupAutocomplete(selector, { setValueOnSelection, scrollSelectionIntoView } = {
  setValueOnSelection: true,
  scrollSelectionIntoView: true
}) {
  const sdInput = !selector ? document.querySelector("#autoComplete") : typeof selector === "string" ? document.querySelector(selector) : selector;
  const input = sdInput.shadowRoot.querySelector("input");
  const css = (string) => string[0];
  input.addEventListener("init", () => {
    var _a, _b;
    const ul = (_a = sdInput.shadowRoot) == null ? void 0 : _a.querySelector("ul");
    ul == null ? void 0 : ul.setAttribute("part", "listbox");
    const popup = document.createElement("sd-popup");
    popup.appendChild(ul);
    (_b = sdInput.shadowRoot) == null ? void 0 : _b.appendChild(popup);
    popup == null ? void 0 : popup.setAttribute("exportparts", "popup__content");
    if (popup) {
      popup.active = false;
      popup.autoSize = "vertical";
      popup.autoSizePadding = 16;
      popup.placement = "bottom-start";
      popup.anchor = sdInput;
      popup.sync = "width";
    }
    const styles = css`/*! tailwindcss v4.3.0 | MIT License | https://tailwindcss.com */@layer properties;@layer theme,base,components,utilities;
sd-popup::part(popup){z-index:var(--sd-z-index-dropdown,900);overflow-y:scroll;border-bottom-right-radius:var(--sd-radius);border-bottom-left-radius:var(--sd-radius);border-style:var(--tw-border-style);border-width:var(--sd-border-width-2);border-top-style:var(--tw-border-style);border-top-width:0;border-color:rgba(var(--sd-color-border-primary));background-color:rgba(var(--sd-color-background-white));padding-inline:var(--sd-spacing-2);padding-block:var(--sd-spacing-3);--tw-shadow:var(--sd-shadow);box-shadow:var(--tw-inset-shadow),var(--tw-inset-ring-shadow),var(--tw-ring-offset-shadow),var(--tw-ring-shadow),var(--tw-shadow)}sd-popup li{transition-property:all;transition-timing-function:var(--tw-ease,var(--default-transition-timing-function,cubic-bezier(.4,0,.2,1)));transition-duration:var(--tw-duration,var(--default-transition-duration,150ms))}sd-popup li:hover:not([disabled]){background-color:rgba(var(--sd-color-background-neutral-100))}sd-popup li{list-style-type:''}sd-popup li mark{background-color:transparent;--tw-font-weight:var(--sd-font-weight-bold);font-weight:var(--sd-font-weight-bold)}sd-popup li:not(:has(sd-option)){cursor:pointer;padding-inline:var(--sd-spacing-4);padding-block:var(--sd-spacing-3);font-size:var(--sd-text-base);line-height:var(--tw-leading,var(--text-base--line-height,calc(1.5 / 1)));color:rgba(var(--sd-color-text-black))}sd-popup li:not(:has(sd-option)):hover{background-color:rgba(var(--sd-color-background-neutral-200))}sd-popup li[aria-selected=true]{background-color:rgba(var(--sd-color-background-neutral-200))}@property --tw-border-style{syntax:"*";inherits:false;initial-value:solid}@property --tw-shadow{syntax:"*";inherits:false;initial-value:0 0 #0000}@property --tw-shadow-color{syntax:"*";inherits:false}@property --tw-shadow-alpha{syntax:"<percentage>";inherits:false;initial-value:100%}@property --tw-inset-shadow{syntax:"*";inherits:false;initial-value:0 0 #0000}@property --tw-inset-shadow-color{syntax:"*";inherits:false}@property --tw-inset-shadow-alpha{syntax:"<percentage>";inherits:false;initial-value:100%}@property --tw-ring-color{syntax:"*";inherits:false}@property --tw-ring-shadow{syntax:"*";inherits:false;initial-value:0 0 #0000}@property --tw-inset-ring-color{syntax:"*";inherits:false}@property --tw-inset-ring-shadow{syntax:"*";inherits:false;initial-value:0 0 #0000}@property --tw-ring-inset{syntax:"*";inherits:false}@property --tw-ring-offset-width{syntax:"<length>";inherits:false;initial-value:0}@property --tw-ring-offset-color{syntax:"*";inherits:false;initial-value:#fff}@property --tw-ring-offset-shadow{syntax:"*";inherits:false;initial-value:0 0 #0000}@property --tw-font-weight{syntax:"*";inherits:false}`;
    const styleSheet = new CSSStyleSheet();
    styleSheet.replaceSync(styles);
    sdInput.shadowRoot.adoptedStyleSheets = [...sdInput.shadowRoot.adoptedStyleSheets, styleSheet];
  });
  if (setValueOnSelection) {
    input.addEventListener("selection", (event) => {
      var _a;
      sdInput.value = (_a = event == null ? void 0 : event.detail) == null ? void 0 : _a.selection.value;
    });
  }
  input.addEventListener("open", () => {
    var _a, _b, _c, _d, _e, _f;
    (_b = (_a = sdInput.shadowRoot) == null ? void 0 : _a.querySelector("sd-popup")) == null ? void 0 : _b.setAttribute("active", "true");
    (_d = (_c = sdInput.shadowRoot) == null ? void 0 : _c.querySelector('[part="border"]')) == null ? void 0 : _d.classList.add("rounded-b-none");
    (_f = (_e = sdInput.shadowRoot) == null ? void 0 : _e.querySelector('[part="form-control"]')) == null ? void 0 : _f.classList.add("z-50");
  });
  input.addEventListener("close", () => {
    var _a, _b, _c, _d, _e, _f;
    (_b = (_a = sdInput.shadowRoot) == null ? void 0 : _a.querySelector("sd-popup")) == null ? void 0 : _b.removeAttribute("active");
    (_d = (_c = sdInput.shadowRoot) == null ? void 0 : _c.querySelector('[part="border"]')) == null ? void 0 : _d.classList.remove("rounded-b-none");
    (_f = (_e = sdInput.shadowRoot) == null ? void 0 : _e.querySelector('[part="form-control"]')) == null ? void 0 : _f.classList.remove("z-50");
  });
  if (scrollSelectionIntoView) {
    input.addEventListener("navigate", () => {
      const selected = sdInput.shadowRoot.querySelector('[aria-selected="true"]');
      selected == null ? void 0 : selected.scrollIntoView({ block: "nearest" });
    });
  }
  return {
    config: {
      selector: () => {
        return input;
      },
      resultsList: {
        tag: "ul"
      },
      wrapper: false
    }
  };
}

export {
  setupAutocomplete
};
