import { defineComponent, mergeModels, useModel, createBlock, openBlock, unref, withCtx, renderSlot, createTextVNode, nextTick } from "vue";
import { useLoadingState, ScalarButton } from "@scalar/components";
import { useToasts } from "@scalar/use-toasts";
import { DASHBOARD_REGISTER_URL } from "../../../consts/urls.js";
import { uploadTempDocument } from "../../../helpers/upload-temp-document.js";
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "ApiReferenceToolbarRegisterButton",
  props: /* @__PURE__ */ mergeModels({
    workspace: {},
    sdks: { default: () => [] }
  }, {
    "url": {},
    "urlModifiers": {}
  }),
  emits: ["update:url"],
  setup(__props) {
    const tempDocUrl = useModel(__props, "url");
    const { toast } = useToasts();
    const loader = useLoadingState();
    function openRegisterLink(docUrl) {
      const url = new URL(DASHBOARD_REGISTER_URL);
      url.searchParams.set("url", docUrl);
      __props.sdks.forEach((sdk) => url.searchParams.append("sdk", sdk));
      window.open(url.toString(), "_blank");
    }
    async function generateRegisterLink() {
      if (loader.isLoading || !__props.workspace) {
        return;
      }
      if (tempDocUrl.value) {
        openRegisterLink(tempDocUrl.value);
        return;
      }
      loader.start();
      const document = __props.workspace.exportActiveDocument("json");
      if (!document) {
        toast("Unable to export active document", "error");
        await loader.invalidate();
        return;
      }
      try {
        tempDocUrl.value = await uploadTempDocument(document);
        await loader.validate();
        openRegisterLink(tempDocUrl.value);
        await nextTick();
        await loader.clear();
      } catch (error) {
        const message = error instanceof Error ? error.message : "An unknown error occurred";
        toast(message, "error");
        await loader.invalidate();
      }
    }
    return (_ctx, _cache) => {
      return openBlock(), createBlock(unref(ScalarButton), {
        class: "h-auto p-2.5",
        loader: unref(loader),
        onClick: generateRegisterLink
      }, {
        default: withCtx(() => [
          renderSlot(_ctx.$slots, "default", {}, () => [
            _cache[0] || (_cache[0] = createTextVNode("Generate", -1))
          ])
        ]),
        _: 3
      }, 8, ["loader"]);
    };
  }
});
export {
  _sfc_main as default
};
