import { defineComponent, mergeModels, useModel, createElementBlock, openBlock, Fragment, createBlock, createVNode, unref, withCtx, createTextVNode } from "vue";
import { useLoadingState, ScalarTextInputCopy, ScalarButton } from "@scalar/components";
import { useToasts } from "@scalar/use-toasts";
import { REGISTRY_SHARE_URL } from "../../../consts/urls.js";
import { uploadTempDocument } from "../../../helpers/upload-temp-document.js";
import ApiReferenceToolbarBlurb from "./ApiReferenceToolbarBlurb.vue.js";
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "ApiReferenceToolbarShareTemporary",
  props: /* @__PURE__ */ mergeModels({
    workspace: {}
  }, {
    "url": {},
    "urlModifiers": {}
  }),
  emits: ["update:url"],
  setup(__props) {
    const { toast } = useToasts();
    const loader = useLoadingState();
    const tempDocUrl = useModel(__props, "url");
    async function generateTemporaryLink() {
      if (loader.isLoading || !__props.workspace || !!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 {
        const url = await uploadTempDocument(document);
        await loader.validate({ duration: 900, persist: true });
        tempDocUrl.value = url;
      } catch (error) {
        const message = error instanceof Error ? error.message : "An unknown error occurred";
        toast(message, "error");
        await loader.invalidate();
      }
    }
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock(Fragment, null, [
        tempDocUrl.value ? (openBlock(), createBlock(unref(ScalarTextInputCopy), {
          key: 0,
          immediate: "",
          modelValue: tempDocUrl.value,
          name: "temporary-link",
          placeholder: `${unref(REGISTRY_SHARE_URL)}/apis/…`
        }, null, 8, ["modelValue", "placeholder"])) : (openBlock(), createBlock(unref(ScalarButton), {
          key: 1,
          class: "h-auto p-2.5",
          loader: unref(loader),
          variant: "gradient",
          onClick: generateTemporaryLink
        }, {
          default: withCtx(() => [..._cache[0] || (_cache[0] = [
            createTextVNode(" Upload Document ", -1)
          ])]),
          _: 1
        }, 8, ["loader"])),
        createVNode(ApiReferenceToolbarBlurb, { class: "-mt-1" }, {
          default: withCtx(() => [..._cache[1] || (_cache[1] = [
            createTextVNode(" Your document will automatically be deleted after 7 days. ", -1)
          ])]),
          _: 1
        })
      ], 64);
    };
  }
});
export {
  _sfc_main as default
};
