'use strict';

var hash = require('@emotion/hash');
require('../../utils/index.cjs');
require('./theme/index.cjs');
var Theme = require('./theme/Theme.cjs');
var canUseDom = require('../../utils/dom/canUseDom.cjs');
var dynamicCSS = require('../../utils/dom/dynamicCSS.cjs');

const flattenTokenCache = /* @__PURE__ */ new WeakMap();
function flattenToken(token) {
  let str = flattenTokenCache.get(token) || "";
  if (!str) {
    Object.keys(token).forEach((key) => {
      const value = token[key];
      str += key;
      if (value instanceof Theme.default)
        str += value.id;
      else if (value && typeof value === "object")
        str += flattenToken(value);
      else
        str += value;
    });
    flattenTokenCache.set(token, str);
  }
  return str;
}
function token2key(token, salt) {
  return hash(`${salt}_${flattenToken(token)}`);
}
const randomSelectorKey = `random-${Date.now()}-${Math.random()}`.replace(/\./g, "");
const checkContent = "_bAmBoO_";
function supportSelector(styleStr, handleElement, supportCheck) {
  var _a, _b;
  if (canUseDom.canUseDom()) {
    dynamicCSS.updateCSS(styleStr, randomSelectorKey);
    const ele = document.createElement("div");
    ele.style.position = "fixed";
    ele.style.left = "0";
    ele.style.top = "0";
    handleElement == null ? void 0 : handleElement(ele);
    document.body.appendChild(ele);
    if (process.env.NODE_ENV !== "production") {
      ele.innerHTML = "Test";
      ele.style.zIndex = "9999999";
    }
    const support = supportCheck ? supportCheck(ele) : (_a = getComputedStyle(ele).content) == null ? void 0 : _a.includes(checkContent);
    (_b = ele.parentNode) == null ? void 0 : _b.removeChild(ele);
    dynamicCSS.removeCSS(randomSelectorKey);
    return support;
  }
  return false;
}
let canLayer;
function supportLayer() {
  if (canLayer === void 0) {
    canLayer = supportSelector(
      `@layer ${randomSelectorKey} { .${randomSelectorKey} { content: "${checkContent}"!important; } }`,
      (ele) => {
        ele.className = randomSelectorKey;
      }
    );
  }
  return canLayer;
}
function withInstall(comp) {
  const c = comp;
  c.install = function(app) {
    app.component(c.displayName || c.name, comp);
  };
  return comp;
}
let canWhere;
function supportWhere() {
  if (canWhere === void 0) {
    canWhere = supportSelector(
      `:where(.${randomSelectorKey}) { content: "${checkContent}"!important; }`,
      (ele) => {
        ele.className = randomSelectorKey;
      }
    );
  }
  return canWhere;
}
let canLogic;
function supportLogicProps() {
  if (canLogic === void 0) {
    canLogic = supportSelector(
      `.${randomSelectorKey} { inset-block: 93px !important; }`,
      (ele) => {
        ele.className = randomSelectorKey;
      },
      (ele) => getComputedStyle(ele).bottom === "93px"
    );
  }
  return canLogic;
}

exports.flattenToken = flattenToken;
exports.supportLayer = supportLayer;
exports.supportLogicProps = supportLogicProps;
exports.supportWhere = supportWhere;
exports.token2key = token2key;
exports.withInstall = withInstall;
//# sourceMappingURL=util.cjs.map
