import m from "./core.js";
let l = !1, c = null;
function p(a, n = document.documentElement, e = null) {
  return n instanceof ShadowRoot && console.warn(
    "[Dialtone] You passed a ShadowRoot directly to setTheme(). Please pass the host element instead. The function will access shadowRoot automatically."
  ), a.base ? u(a, n, e) : r(a, n);
}
function u(a, n = document.documentElement, e = null) {
  b(a.base.name, a.brand.name, n), n != null && n.shadowRoot && (n = n.shadowRoot), s("dialtone-css-theme", a.base.css, n), s("dialtone-css-brand", a.brand.css, n), e ? (s("dialtone-css-contrast", e.css, n), n == null || n.setAttribute("data-dt-contrast", "high")) : (i("dialtone-css-contrast", n), n == null || n.setAttribute("data-dt-contrast", "default"));
}
function r(a, n = document.documentElement) {
  n != null && n.shadowRoot && (n = n.shadowRoot), a.core && !l && (s("dialtone-css-core", a.core, n), l = !0), a.baseColors && !(n != null && n.querySelector("#dialtone-css-base-colors")) && s("dialtone-css-base-colors", a.baseColors, n), a.brand && (s("dialtone-css-brand-colors", a.brand.css, n), n == null || n.setAttribute("data-dt-brand", a.brand.name)), a.contrast && (s("dialtone-css-contrast", a.contrast.css, n), n == null || n.setAttribute("data-dt-contrast", a.contrast.name));
}
function s(a, n, e) {
  if (e != null && e.querySelector("#" + a)) {
    const t = e.querySelector("#" + a);
    t.innerHTML = n;
  } else {
    const t = document.createElement("style");
    t.setAttribute("type", "text/css"), t.setAttribute("id", a), t.innerHTML = n, e != null && e.querySelector("head") ? e.querySelector("head").appendChild(t) : e == null || e.appendChild(t);
  }
}
function i(a, n) {
  const e = n == null ? void 0 : n.querySelector("#" + a);
  e && e.remove();
}
function b(a, n, e) {
  e == null || e.setAttribute("data-dt-theme", a), e == null || e.setAttribute("data-dt-brand", n);
}
function h(a, n = document.documentElement) {
  if (a !== "light" && a !== "dark") {
    console.warn(`Invalid mode: ${a}. Must be 'light' or 'dark'`);
    return;
  }
  n instanceof ShadowRoot && console.warn(
    "[Dialtone] You passed a ShadowRoot directly to setMode(). Please pass the host element instead. The function will access shadowRoot automatically."
  ), n != null && n.shadowRoot && (n = n.shadowRoot), n == null || n.setAttribute("data-dt-mode", a);
}
function f(a, n = document.documentElement) {
  if (!a || typeof a != "object")
    throw new TypeError(
      "[Dialtone] setBrand: brandTheme must be an object. Import a brand theme like: import Dp from '@dialpad/dialtone-tokens/themes/dp';"
    );
  if (!a.brand || typeof a.brand != "object")
    throw new TypeError(
      "[Dialtone] setBrand: brandTheme.brand must be an object with {name, css} properties."
    );
  if (typeof a.brand.name != "string" || !a.brand.name)
    throw new TypeError(
      "[Dialtone] setBrand: brandTheme.brand.name must be a non-empty string."
    );
  if (typeof a.brand.css != "string")
    throw new TypeError(
      "[Dialtone] setBrand: brandTheme.brand.css must be a string containing CSS."
    );
  n instanceof ShadowRoot && console.warn(
    "[Dialtone] You passed a ShadowRoot directly to setBrand(). Please pass the host element instead. The function will access shadowRoot automatically."
  ), n != null && n.shadowRoot && (n = n.shadowRoot), s("dialtone-css-brand-colors", a.brand.css, n), n == null || n.setAttribute("data-dt-brand", a.brand.name);
}
function y(a, n = document.documentElement) {
  n instanceof ShadowRoot && console.warn(
    "[Dialtone] You passed a ShadowRoot directly to setContrast(). Please pass the host element instead. The function will access shadowRoot automatically."
  ), n != null && n.shadowRoot && (n = n.shadowRoot), a && a.contrast ? (s("dialtone-css-contrast", a.contrast.css, n), n == null || n.setAttribute("data-dt-contrast", a.contrast.name)) : (i("dialtone-css-contrast", n), n == null || n.setAttribute("data-dt-contrast", "default"));
}
function g(a, n = "light", e = document.documentElement) {
  if (!a || typeof a != "object")
    throw new TypeError(
      "[Dialtone] initDialtoneTheme: brandTheme must be an object. Import a brand theme like: import Dp from '@dialpad/dialtone-tokens/themes/dp';"
    );
  if (!a.brand || typeof a.brand != "object")
    throw new TypeError(
      "[Dialtone] initDialtoneTheme: brandTheme.brand must be an object with {name, css} properties. Ensure you're importing a valid brand theme."
    );
  if (typeof a.brand.name != "string" || !a.brand.name)
    throw new TypeError(
      "[Dialtone] initDialtoneTheme: brandTheme.brand.name must be a non-empty string."
    );
  if (typeof a.brand.css != "string")
    throw new TypeError(
      "[Dialtone] initDialtoneTheme: brandTheme.brand.css must be a string containing CSS."
    );
  if (n !== "light" && n !== "dark")
    throw new TypeError(
      `[Dialtone] initDialtoneTheme: mode must be 'light' or 'dark', got '${n}'.`
    );
  if (!e || typeof e != "object")
    throw new TypeError(
      "[Dialtone] initDialtoneTheme: rootNode must be an HTMLElement or host element with shadowRoot."
    );
  if (e instanceof ShadowRoot && console.warn(
    `[Dialtone] You passed a ShadowRoot directly to initDialtoneTheme(). Please pass the host element instead. The function will access shadowRoot automatically.
Correct: initDialtoneTheme(brand, mode, hostElement)
Incorrect: initDialtoneTheme(brand, mode, hostElement.shadowRoot)`
  ), e != null && e.shadowRoot && (e = e.shadowRoot), e === document.documentElement && document.querySelector("#dialtone-css-core") !== null)
    throw new Error(
      "[Dialtone] Cannot initialize theme on document.documentElement because theme styles already exist. You are likely in an embedded app/micro-frontend. You MUST pass your own container element as the rootNode parameter. Example: initDialtoneTheme(brandTheme, mode, myAppContainerElement)"
    );
  const t = c;
  if (t)
    if (t.brand === a.brand.name && t.mode === n) {
      console.warn(
        `[Dialtone] Theme already initialized with brand '${a.brand.name}' and mode '${n}'. Re-applying the same theme may be unnecessary. If you need to switch themes dynamically, use setBrand() or setMode() instead of calling initDialtoneTheme() again.`
      );
      return;
    } else {
      console.warn(
        `[Dialtone] Theme already initialized. Previous: brand='${t.brand}', mode='${t.mode}'. New: brand='${a.brand.name}', mode='${n}'. Re-initializing with different parameters. Consider using setBrand()/setMode() for dynamic switching.`
      );
      return;
    }
  s("dialtone-css-core", m.core, e), l = !0, s("dialtone-css-base-colors", m.baseColors, e), h(n, e), f(a, e), e == null || e.setAttribute("data-dt-contrast", "default"), c = {
    brand: a.brand.name,
    mode: n,
    contrast: "default"
  };
}
function D(a = document.documentElement) {
  const n = (a == null ? void 0 : a.shadowRoot) || a;
  c = null, l = !1, i("dialtone-css-core", n), i("dialtone-css-base-colors", n), i("dialtone-css-brand-colors", n), i("dialtone-css-contrast", n), n == null || n.removeAttribute("data-dt-mode"), n == null || n.removeAttribute("data-dt-brand"), n == null || n.removeAttribute("data-dt-contrast");
}
export {
  g as initDialtoneTheme,
  D as resetTheme,
  f as setBrand,
  y as setContrast,
  h as setMode,
  p as setTheme
};
