import { safeLocalStorage as d, REFERENCE_LS_KEYS as u } from "@scalar/helpers/object/local-storage";
import { generateClientMutators as n } from "@scalar/workspace-store/client";
import { onCustomEvent as c } from "@scalar/workspace-store/events";
const f = (r, t, i) => {
  const v = n(r);
  c(t, "scalar-update-dark-mode", (e) => {
    r.update("x-scalar-dark-mode", e.detail.value);
  }), c(t, "scalar-update-active-document", (e) => {
    r.update("x-scalar-active-document", e.detail.value);
  }), c(t, "scalar-update-selected-client", (e) => {
    r.update("x-scalar-default-client", e.detail), d().setItem(u.SELECTED_CLIENT, e.detail);
  }), c(t, "scalar-replace-servers", (e) => {
    const a = r.workspace.activeDocument;
    a && (a.servers = e.detail.servers, a["x-scalar-active-server"] = e.detail.servers.at(-1)?.url);
  }), c(t, "scalar-update-selected-server", (e) => {
    const a = r.workspace.activeDocument;
    a && (a["x-scalar-active-server"] = e.detail.value), i.onServerChange && i.onServerChange(e.detail.value ?? "");
  }), c(t, "store-update-selected-server-properties", (e) => {
    const a = r.workspace.activeDocument;
    if (!a)
      return;
    const s = a.servers?.find((l) => l.url === a["x-scalar-active-server"]);
    s && (s[e.detail.key] = e.detail.value);
  }), c(t, "scalar-update-selected-server-variables", (e) => {
    const a = r.workspace.activeDocument;
    if (!a)
      return;
    const s = a.servers?.find((l) => l.url === a["x-scalar-active-server"]);
    s && s.variables?.[e.detail.key] && (s.variables[e.detail.key].default = e.detail.value);
  }), c(t, "scalar-add-server", (e) => {
    v.active().serverMutators.addServer(e.detail.server);
    const a = r.workspace.activeDocument;
    a && (a["x-scalar-active-server"] = e.detail.server.url);
  }), c(t, "scalar-delete-server", (e) => {
    v.active().serverMutators.deleteServer(e.detail.url);
  });
};
export {
  f as useWorkspaceStoreEvents
};
