/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import { makeGenericController } from "@arcgis/lumina/controllers";
const useTopLayer = (options) => {
  return makeGenericController((component, controller) => {
    let opened = false;
    controller.onConnected(() => {
      if (opened) {
        togglePopover(true);
      }
    });
    async function togglePopover(open) {
      await component.componentOnReady();
      const nativePopoverEl = typeof options.target === "function" ? options.target() : options.target.value;
      if (!nativePopoverEl || !nativePopoverEl.hasAttribute("popover")) {
        return;
      }
      const isDisabled = options.disabledOverride?.() || "topLayerDisabled" in component && component.topLayerDisabled === true;
      if (isDisabled || !open) {
        opened = false;
        nativePopoverEl.hidePopover();
        return;
      }
      opened = true;
      nativePopoverEl.showPopover();
    }
    return {
      show: async () => {
        await togglePopover(true);
      },
      hide: async () => {
        await togglePopover(false);
      }
    };
  });
};
export {
  useTopLayer as u
};
