import { defineComponent, useTemplateRef, createElementBlock, openBlock, Fragment, createElementVNode, createBlock, createCommentVNode, createVNode, unref, withCtx, mergeProps, createTextVNode } from "vue";
import { DataTableRow } from "@scalar/api-client/components/DataTable";
import { useWorkspace } from "@scalar/api-client/store";
import { authorizeOauth2 } from "@scalar/api-client/views/Request/libs";
import { useLoadingState, ScalarButton } from "@scalar/components";
import { pkceOptions } from "@scalar/oas-utils/entities/spec";
import { useToasts } from "@scalar/use-toasts";
import { updateScheme } from "../helpers/update-scheme.js";
import _sfc_main$2 from "./OAuthScopesInput.vue.js";
import _sfc_main$1 from "./RequestAuthDataTableInput.vue.js";
const _hoisted_1 = { class: "flex h-8 items-center justify-end gap-2 border-t" };
const _hoisted_2 = { class: "flex h-8 w-full items-center justify-end border-t" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "OAuth2",
  props: {
    collection: {},
    environment: {},
    envVariables: {},
    flow: {},
    persistAuth: { type: Boolean, default: false },
    scheme: {},
    server: {},
    workspace: {}
  },
  setup(__props) {
    const loader = useLoadingState();
    const { toast } = useToasts();
    const storeContext = useWorkspace();
    const updateScheme$1 = (path, value) => updateScheme(
      __props.scheme.uid,
      path,
      value,
      storeContext,
      wrapperRef.value,
      __props.persistAuth
    );
    const handleAuthorize = async () => {
      if (loader.isLoading || !__props.collection?.uid) {
        return;
      }
      if (!__props.server) {
        toast("No server selected", "error");
        return;
      }
      loader.start();
      const [error, accessToken] = await authorizeOauth2(
        __props.flow,
        __props.server,
        __props.workspace?.proxyUrl
      ).finally(async () => {
        await loader.clear();
      });
      if (accessToken) {
        updateScheme$1(`flows.${__props.flow.type}.token`, accessToken);
      } else {
        console.error(error);
        toast(error?.message ?? "Failed to authorize", "error");
      }
    };
    const dataTableInputProps = {
      environment: __props.environment,
      envVariables: __props.envVariables,
      workspace: __props.workspace
    };
    const wrapperRef = useTemplateRef("wrapperRef");
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock(Fragment, null, [
        createElementVNode("div", {
          ref_key: "wrapperRef",
          ref: wrapperRef
        }, null, 512),
        _ctx.flow.token ? (openBlock(), createElementBlock(Fragment, { key: 0 }, [
          createVNode(unref(DataTableRow), null, {
            default: withCtx(() => [
              createVNode(_sfc_main$1, mergeProps(dataTableInputProps, {
                class: "border-r-transparent",
                modelValue: _ctx.flow.token,
                placeholder: "QUxMIFlPVVIgQkFTRSBBUkUgQkVMT05HIFRPIFVT",
                type: "password",
                "onUpdate:modelValue": _cache[0] || (_cache[0] = (v) => updateScheme$1(`flows.${_ctx.flow.type}.token`, v))
              }), {
                default: withCtx(() => [..._cache[10] || (_cache[10] = [
                  createTextVNode(" Access Token ", -1)
                ])]),
                _: 1
              }, 16, ["modelValue"])
            ]),
            _: 1
          }),
          createVNode(unref(DataTableRow), { class: "min-w-full" }, {
            default: withCtx(() => [
              createElementVNode("div", _hoisted_1, [
                createVNode(unref(ScalarButton), {
                  class: "mr-1 p-0 px-2 py-0.5",
                  loader: unref(loader),
                  size: "sm",
                  variant: "outlined",
                  onClick: _cache[1] || (_cache[1] = ($event) => updateScheme$1(`flows.${_ctx.flow.type}.token`, ""))
                }, {
                  default: withCtx(() => [..._cache[11] || (_cache[11] = [
                    createTextVNode(" Clear ", -1)
                  ])]),
                  _: 1
                }, 8, ["loader"])
              ])
            ]),
            _: 1
          })
        ], 64)) : (openBlock(), createElementBlock(Fragment, { key: 1 }, [
          createVNode(unref(DataTableRow), null, {
            default: withCtx(() => [
              "authorizationUrl" in _ctx.flow ? (openBlock(), createBlock(_sfc_main$1, mergeProps({ key: 0 }, dataTableInputProps, {
                containerClass: "border-r-0",
                modelValue: _ctx.flow.authorizationUrl,
                placeholder: "https://galaxy.scalar.com/authorize",
                "onUpdate:modelValue": _cache[2] || (_cache[2] = (v) => updateScheme$1(`flows.${_ctx.flow.type}.authorizationUrl`, v))
              }), {
                default: withCtx(() => [..._cache[12] || (_cache[12] = [
                  createTextVNode(" Auth URL ", -1)
                ])]),
                _: 1
              }, 16, ["modelValue"])) : createCommentVNode("", true),
              "tokenUrl" in _ctx.flow ? (openBlock(), createBlock(_sfc_main$1, mergeProps({ key: 1 }, dataTableInputProps, {
                modelValue: _ctx.flow.tokenUrl,
                placeholder: "https://galaxy.scalar.com/token",
                "onUpdate:modelValue": _cache[3] || (_cache[3] = (v) => updateScheme$1(`flows.${_ctx.flow.type}.tokenUrl`, v))
              }), {
                default: withCtx(() => [..._cache[13] || (_cache[13] = [
                  createTextVNode(" Token URL ", -1)
                ])]),
                _: 1
              }, 16, ["modelValue"])) : createCommentVNode("", true)
            ]),
            _: 1
          }),
          "x-scalar-redirect-uri" in _ctx.flow ? (openBlock(), createBlock(unref(DataTableRow), { key: 0 }, {
            default: withCtx(() => [
              createVNode(_sfc_main$1, mergeProps(dataTableInputProps, {
                modelValue: _ctx.flow["x-scalar-redirect-uri"],
                placeholder: "https://galaxy.scalar.com/callback",
                "onUpdate:modelValue": _cache[4] || (_cache[4] = (v) => updateScheme$1(`flows.${_ctx.flow.type}.x-scalar-redirect-uri`, v))
              }), {
                default: withCtx(() => [..._cache[14] || (_cache[14] = [
                  createTextVNode(" Redirect URL ", -1)
                ])]),
                _: 1
              }, 16, ["modelValue"])
            ]),
            _: 1
          })) : createCommentVNode("", true),
          _ctx.flow.type === "password" ? (openBlock(), createElementBlock(Fragment, { key: 1 }, [
            createVNode(unref(DataTableRow), null, {
              default: withCtx(() => [
                createVNode(_sfc_main$1, mergeProps(dataTableInputProps, {
                  class: "text-c-2",
                  modelValue: _ctx.flow.username,
                  placeholder: "janedoe",
                  "onUpdate:modelValue": _cache[5] || (_cache[5] = (v) => updateScheme$1(`flows.${_ctx.flow.type}.username`, v))
                }), {
                  default: withCtx(() => [..._cache[15] || (_cache[15] = [
                    createTextVNode(" Username ", -1)
                  ])]),
                  _: 1
                }, 16, ["modelValue"])
              ]),
              _: 1
            }),
            createVNode(unref(DataTableRow), null, {
              default: withCtx(() => [
                createVNode(_sfc_main$1, mergeProps(dataTableInputProps, {
                  modelValue: _ctx.flow.password,
                  placeholder: "********",
                  type: "password",
                  "onUpdate:modelValue": _cache[6] || (_cache[6] = (v) => updateScheme$1(`flows.${_ctx.flow.type}.password`, v))
                }), {
                  default: withCtx(() => [..._cache[16] || (_cache[16] = [
                    createTextVNode(" Password ", -1)
                  ])]),
                  _: 1
                }, 16, ["modelValue"])
              ]),
              _: 1
            })
          ], 64)) : createCommentVNode("", true),
          createVNode(unref(DataTableRow), null, {
            default: withCtx(() => [
              createVNode(_sfc_main$1, mergeProps(dataTableInputProps, {
                modelValue: _ctx.flow["x-scalar-client-id"],
                placeholder: "12345",
                "onUpdate:modelValue": _cache[7] || (_cache[7] = (v) => updateScheme$1(`flows.${_ctx.flow.type}.x-scalar-client-id`, v))
              }), {
                default: withCtx(() => [..._cache[17] || (_cache[17] = [
                  createTextVNode(" Client ID ", -1)
                ])]),
                _: 1
              }, 16, ["modelValue"])
            ]),
            _: 1
          }),
          "clientSecret" in _ctx.flow ? (openBlock(), createBlock(unref(DataTableRow), { key: 2 }, {
            default: withCtx(() => [
              createVNode(_sfc_main$1, mergeProps(dataTableInputProps, {
                modelValue: _ctx.flow.clientSecret,
                placeholder: "XYZ123",
                type: "password",
                "onUpdate:modelValue": _cache[8] || (_cache[8] = (v) => updateScheme$1(`flows.${_ctx.flow.type}.clientSecret`, v))
              }), {
                default: withCtx(() => [..._cache[18] || (_cache[18] = [
                  createTextVNode(" Client Secret ", -1)
                ])]),
                _: 1
              }, 16, ["modelValue"])
            ]),
            _: 1
          })) : createCommentVNode("", true),
          "x-usePkce" in _ctx.flow ? (openBlock(), createBlock(unref(DataTableRow), { key: 3 }, {
            default: withCtx(() => [
              createVNode(_sfc_main$1, mergeProps(dataTableInputProps, {
                enum: unref(pkceOptions),
                modelValue: _ctx.flow["x-usePkce"],
                readOnly: "",
                "onUpdate:modelValue": _cache[9] || (_cache[9] = (v) => updateScheme$1(
                  `flows.${_ctx.flow.type}.x-usePkce`,
                  v
                ))
              }), {
                default: withCtx(() => [..._cache[19] || (_cache[19] = [
                  createTextVNode(" Use PKCE ", -1)
                ])]),
                _: 1
              }, 16, ["enum", "modelValue"])
            ]),
            _: 1
          })) : createCommentVNode("", true),
          Object.keys(_ctx.flow.scopes ?? {}).length ? (openBlock(), createBlock(unref(DataTableRow), { key: 4 }, {
            default: withCtx(() => [
              createVNode(_sfc_main$2, {
                flow: _ctx.flow,
                updateScheme: updateScheme$1
              }, null, 8, ["flow"])
            ]),
            _: 1
          })) : createCommentVNode("", true)
        ], 64)),
        !_ctx.flow.token ? (openBlock(), createBlock(unref(DataTableRow), {
          key: 2,
          class: "min-w-full"
        }, {
          default: withCtx(() => [
            createElementVNode("div", _hoisted_2, [
              createVNode(unref(ScalarButton), {
                class: "mr-0.75 p-0 px-2 py-0.5",
                loader: unref(loader),
                size: "sm",
                variant: "outlined",
                onClick: handleAuthorize
              }, {
                default: withCtx(() => [..._cache[20] || (_cache[20] = [
                  createTextVNode(" Authorize ", -1)
                ])]),
                _: 1
              }, 8, ["loader"])
            ])
          ]),
          _: 1
        })) : createCommentVNode("", true)
      ], 64);
    };
  }
});
export {
  _sfc_main as default
};
