import '../../../../utils/index.mjs';
import { ATTR_MARK } from '../../StyleContext.mjs';
import { canUseDom } from '../../../../utils/dom/canUseDom.mjs';

const ATTR_CACHE_MAP = "data-ant-cssinjs-cache-path";
const CSS_FILE_STYLE = "_FILE_STYLE__";
function serialize(cachePathMap2) {
  return Object.keys(cachePathMap2).map((path) => {
    const hash = cachePathMap2[path];
    return `${path}:${hash}`;
  }).join(";");
}
let cachePathMap;
let fromCSSFile = true;
function reset(mockCache, fromFile = true) {
  cachePathMap = mockCache;
  fromCSSFile = fromFile;
}
function prepare() {
  var _a;
  if (!cachePathMap) {
    cachePathMap = {};
    if (canUseDom()) {
      const div = document.createElement("div");
      div.className = ATTR_CACHE_MAP;
      div.style.position = "fixed";
      div.style.visibility = "hidden";
      div.style.top = "-9999px";
      document.body.appendChild(div);
      let content = getComputedStyle(div).content || "";
      content = content.replace(/^"/, "").replace(/"$/, "");
      content.split(";").forEach((item) => {
        const [path, hash] = item.split(":");
        cachePathMap[path] = hash;
      });
      const inlineMapStyle = document.querySelector(`style[${ATTR_CACHE_MAP}]`);
      if (inlineMapStyle) {
        fromCSSFile = false;
        (_a = inlineMapStyle.parentNode) == null ? void 0 : _a.removeChild(inlineMapStyle);
      }
      document.body.removeChild(div);
    }
  }
}
function existPath(path) {
  prepare();
  return !!cachePathMap[path];
}
function getStyleAndHash(path) {
  const hash = cachePathMap[path];
  let styleStr = null;
  if (hash && canUseDom()) {
    if (fromCSSFile) {
      styleStr = CSS_FILE_STYLE;
    } else {
      const style = document.querySelector(`style[${ATTR_MARK}="${cachePathMap[path]}"]`);
      if (style) {
        styleStr = style.innerHTML;
      } else {
        delete cachePathMap[path];
      }
    }
  }
  return [styleStr, hash];
}

export { ATTR_CACHE_MAP, CSS_FILE_STYLE, existPath, getStyleAndHash, prepare, reset, serialize };
//# sourceMappingURL=cacheMapUtil.mjs.map
