/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import { i as c, b as u, f as y, L as m, E as g } from "./language-defaults-base.js";
import { CompletionItemKind as h, InsertTextFormat as P, InsertTextMode as F } from "vscode-languageserver-types";
import { fetchT9nStringsBundle as M } from "@arcgis/toolkit/intl";
import { g as b } from "./runtime.js";
const x = (r) => {
  const t = r.variables[0], i = t?.type === "featureSet" ? A(t) : void 0, { apiVersion: e, bundles: o, hiddenApiItems: s } = r.toEditorProfileDefinition();
  return {
    apiVersion: e,
    bundles: o,
    variables: i ? [i] : [],
    hiddenApiItems: s?.map((a) => a.toLowerCase())
  };
};
function A(r) {
  const { name: t } = r, i = r.getDescription(), { definition: e } = r.toProfileVariableDefinition(), o = {
    label: t,
    detail: t,
    insertText: t,
    insertTextMode: F.asIs,
    insertTextFormat: P.PlainText,
    kind: h.Field
  }, s = {
    name: t,
    description: i,
    type: "dictionary",
    properties: [],
    completion: o
  };
  if (!e || !("fields" in e))
    return s;
  const [a, l] = y(e.fields, i);
  return s.properties = a, o.documentation = { kind: "markdown", value: l }, s;
}
const T = {
  id: "field-calculation",
  variables: [
    {
      name: "$layer",
      type: "featureSet",
      description: "The layer being calculated."
    }
  ]
};
function v() {
  return T;
}
function E(r) {
  if (!c(r))
    return;
  const t = v(
    /*locale*/
  );
  if (!t)
    return;
  const i = {
    variables: [],
    hiddenApiItems: r.hiddenApiItems?.map((e) => e.toLowerCase())
  };
  return t.variables.forEach((e) => {
    if (r.disabledVariables?.includes(e.name))
      return;
    const o = r.definitions[e.name];
    switch (e.type) {
      case "featureSet":
        return u(o) ? i.variables.push({ ...e, type: e.type, definition: o }) : void 0;
      default:
        throw Error(`sdkVariable type not supported (yet) type: ${e.type}`);
    }
  }), i;
}
const d = "arcgis-sql-expression";
class w extends m {
  constructor() {
    super(d, {
      apiPath: "./assets/sql-language/api",
      apiPrefix: "sql-api.t9n."
    }), this._languageId = d, this._layerMap = /* @__PURE__ */ new Map();
  }
  // TODO: try to rely on editor profile instance instead of storing a feature layer
  updateFeatureLayerForModel(t, i) {
    const e = this._getApiKey(t);
    this._layerMap.set(e, i);
  }
  getFeatureLayerForModel(t) {
    const i = this._getApiKey(t);
    return this._layerMap.get(i);
  }
  async setProfileForModel(t, i, e = { locale: "en" }) {
    c(i) && (i = E(i));
    const o = await M(
      e.locale,
      b("./assets/editor-profile/t9n"),
      "messages."
    );
    if (!o)
      throw new Error(`Failed to load the language bundle for ${e.locale}`);
    this.disposeForModel(t);
    const s = this._getApiKey(t), a = new g(i, o);
    this._modelToProfileMap.set(s, a);
    const l = x(a);
    this.updateApiContextForModel(t, {
      locale: e.locale,
      profile: l
    });
    const p = i?.variables?.find((f) => f.name === "$layer");
    p && this.updateFeatureLayerForModel(t, p.definition);
  }
}
const n = new w(), q = {
  setProfileForModel: n.setProfileForModel.bind(n),
  getApiLibraryForModel: n.getApiLibraryForModel.bind(n)
};
export {
  n as a,
  q as s
};
