import { defineComponent as L, ref as T, computed as h, watchEffect as E, createElementBlock as v, openBlock as i, Fragment as b, createVNode as n, createSlots as F, withCtx as u, createElementVNode as r, normalizeClass as U, renderList as j, createBlock as M, unref as s, toDisplayString as W, createCommentVNode as H, createTextVNode as d } from "vue";
import { useModal as q, ScalarMarkdown as G, ScalarButton as O, ScalarToggle as J, ScalarModal as K } from "@scalar/components";
import { debounce as Q } from "@scalar/helpers/general/debounce";
import { isHttpMethod as D } from "@scalar/helpers/http/is-http-method";
import { ScalarIconTrash as X, ScalarIconPlus as Y } from "@scalar/icons";
import { getResolvedRef as Z } from "@scalar/workspace-store/helpers/get-resolved-ref";
import _ from "../../../../components/Sidebar/Actions/DeleteSidebarListElement.vue.js";
import ee from "../../settings/components/Section.vue.js";
import te from "./Form.vue.js";
import re from "../../../../components/Server/ServerVariablesForm.vue.js";
const le = { class: "flex h-8 items-center" }, oe = { class: "flex flex-col gap-4" }, ae = { class: "bg-b-2 flex items-center justify-between rounded-t-lg px-3 py-1 text-sm" }, ne = {
  key: 1,
  class: "self-center"
}, se = { class: "divide-0 flex w-full flex-col divide-y rounded-b-lg text-sm" }, ie = { class: "text-c-3 flex h-full items-center justify-center rounded-lg border p-4" }, ge = /* @__PURE__ */ L({
  __name: "Servers",
  props: {
    documentSlug: {},
    document: {},
    eventBus: {},
    layout: {},
    path: {},
    method: {},
    exampleName: {},
    environment: {},
    securitySchemes: {},
    workspaceStore: {},
    activeWorkspace: {},
    plugins: {},
    customThemes: {},
    currentTheme: {},
    isDarkMode: { type: Boolean },
    fetchRegistryDocument: { type: Function },
    collectionType: {}
  },
  setup(t) {
    const g = q(), c = T(-1), m = h(() => t.collectionType === "operation"), S = h(() => !m.value || !t.path || !D(t.method) ? null : Z(t.document?.paths?.[t.path]?.[t.method])), f = T(!1);
    E(() => {
      f.value = S.value?.servers !== void 0;
    });
    const k = h(() => t.collectionType === "operation" && S.value ? S.value.servers ?? [] : t.document?.servers ?? []), C = h(() => k.value[c.value]), a = h(() => t.collectionType === "operation" && t.path && D(t.method) ? { type: "operation", path: t.path, method: t.method } : { type: "document" }), N = [
      {
        label: "URL",
        key: "url",
        placeholder: "https://void.scalar.com"
      },
      {
        label: "Description",
        key: "description",
        placeholder: "Production"
      }
    ], R = (l) => {
      c.value = l, g.show();
    }, w = () => {
      g.hide(), c.value = -1;
    }, V = () => {
      c.value < 0 || (t.eventBus.emit("server:delete:server", {
        index: c.value,
        meta: a.value
      }), w());
    }, { execute: $ } = Q({ delay: 328, maxWait: 1e3 }), A = (l, e, o) => $(
      `${l}-${e}-${a.value.type}`,
      () => t.eventBus.emit("server:update:server", {
        index: l,
        server: { [e]: o },
        meta: a.value
      })
    ), I = (l, e, o) => $(
      `${l}-${e}-${a.value.type}`,
      () => t.eventBus.emit("server:update:variables", {
        index: l,
        key: e,
        value: o,
        meta: a.value
      })
    ), z = () => t.eventBus.emit("server:add:server", { meta: a.value }), B = (l, e = 0) => l?.description || `Server ${e + 1}`, P = (l) => {
      if (a.value.type === "operation")
        return f.value = l, l ? t.eventBus.emit("server:initialize:servers", {
          meta: a.value
        }) : t.eventBus.emit("server:clear:servers", { meta: a.value });
    };
    return (l, e) => (i(), v(b, null, [
      n(ee, null, F({
        title: u(() => [
          e[0] || (e[0] = d("Servers", -1))
        ]),
        description: u(() => [
          m.value ? (i(), v(b, { key: 0 }, [
            e[1] || (e[1] = r("span", { class: "block" }, " Override servers for this operation with the toggle. ", -1)),
            e[2] || (e[2] = r("span", { class: "mt-1 block" }, [
              r("strong", null, "On"),
              d(" — Servers below apply only to this operation. ")
            ], -1)),
            e[3] || (e[3] = r("span", { class: "mt-1 block" }, [
              r("strong", null, "Off"),
              d(" — Removes operation servers; this operation uses document or path servers from the OpenAPI spec. ")
            ], -1)),
            e[4] || (e[4] = r("span", { class: "text-c-3 mt-1 block" }, [
              d(" Use "),
              r("code", { class: "font-code text-c-2" }, "{variables}"),
              d(" for dynamic URL parts. ")
            ], -1))
          ], 64)) : (i(), v(b, { key: 1 }, [
            e[5] || (e[5] = d(" Add different base URLs for your API. Use ", -1)),
            e[6] || (e[6] = r("code", { class: "font-code text-c-2" }, "{variables}", -1)),
            e[7] || (e[7] = d(" for dynamic parts. ", -1))
          ], 64))
        ]),
        default: u(() => [
          r("div", {
            class: U(m.value && !f.value && "cursor-not-allowed")
          }, [
            r("div", {
              class: U([
                "flex flex-col gap-4",
                m.value && !f.value && "pointer-events-none cursor-not-allowed opacity-50 mix-blend-luminosity"
              ])
            }, [
              r("div", oe, [
                (i(!0), v(b, null, j(k.value, (o, p) => (i(), v("div", {
                  key: p,
                  class: "rounded-lg border"
                }, [
                  r("div", ae, [
                    o.description ? (i(), M(s(G), {
                      key: 0,
                      class: "self-center",
                      value: o.description
                    }, null, 8, ["value"])) : (i(), v("span", ne, W(B(o, p)), 1)),
                    n(s(O), {
                      class: "hover:bg-b-3 hover:text-c-1 h-fit p-1.25",
                      "data-testid": "delete-server-button",
                      variant: "ghost",
                      onClick: (y) => R(p)
                    }, {
                      default: u(() => [
                        n(s(X), { class: "size-3.5" })
                      ]),
                      _: 1
                    }, 8, ["onClick"])
                  ]),
                  r("div", se, [
                    n(te, {
                      data: o,
                      environment: t.environment,
                      onUpdate: (y, x) => A(p, y, x),
                      options: N
                    }, null, 8, ["data", "environment", "onUpdate"]),
                    o.variables ? (i(), M(s(re), {
                      key: 0,
                      variables: o.variables,
                      "onUpdate:variable": (y, x) => I(p, y, x)
                    }, null, 8, ["variables", "onUpdate:variable"])) : H("", !0)
                  ])
                ]))), 128))
              ]),
              r("div", ie, [
                n(s(O), {
                  class: "hover:bg-b-2 hover:text-c-1 flex items-center gap-2",
                  size: "sm",
                  variant: "ghost",
                  onClick: z
                }, {
                  default: u(() => [
                    n(s(Y)),
                    e[8] || (e[8] = r("span", null, "Add Server", -1))
                  ]),
                  _: 1
                })
              ])
            ], 2)
          ], 2)
        ]),
        _: 2
      }, [
        m.value ? {
          name: "actions",
          fn: u(() => [
            r("div", le, [
              n(s(J), {
                class: "w-4",
                modelValue: f.value,
                "onUpdate:modelValue": P
              }, null, 8, ["modelValue"])
            ])
          ]),
          key: "0"
        } : void 0
      ]), 1024),
      n(s(K), {
        size: "xxs",
        state: s(g),
        title: `Delete ${B(C.value, c.value)}`
      }, {
        default: u(() => [
          n(_, {
            variableName: "Server",
            warningMessage: "Are you sure you want to delete this server? This action cannot be undone.",
            onClose: w,
            onDelete: V
          })
        ]),
        _: 1
      }, 8, ["state", "title"])
    ], 64));
  }
});
export {
  ge as default
};
