import { createApp } from "vue";
import PublicHomeDefault from "./PublicHomeDefault.vue";
import { hydrateAppSettings } from "../composables/useAppSettings.js";

/**
 * Mount a public homepage app outside the authenticated Maestro shell.
 *
 * @param {{
 *   component?: import("vue").Component,
 *   selector?: string,
 *   props?: Record<string, unknown>,
 *   hydrateSettings?: boolean,
 * }} [options]
 */
export async function mountPublicHome(options = {}) {
  const {
    component = PublicHomeDefault,
    selector = "#public-app",
    props = {},
    hydrateSettings = true,
  } = options;

  if (hydrateSettings) {
    await hydrateAppSettings();
  }

  const target = document.querySelector(selector);
  if (!target) {
    throw new Error(`mountPublicHome: target "${selector}" was not found.`);
  }

  const app = createApp(component, props);
  app.mount(target);

  return app;
}

export { PublicHomeDefault };
