import { getCurrentInstance, inject, shallowRef, watch, unref, provide, defineComponent } from 'vue';
import Entity from './Cache.mjs';
import { withInstall } from './util.mjs';

const ATTR_TOKEN = "data-token-hash";
const ATTR_MARK = "data-css-hash";
const ATTR_CACHE_PATH = "data-cache-path";
const CSS_IN_JS_INSTANCE = "__cssinjs_instance__";
function createCache() {
  const cssinjsInstanceId = Math.random().toString(12).slice(2);
  if (typeof document !== "undefined" && document.head && document.body) {
    const styles = document.body.querySelectorAll(`style[${ATTR_MARK}]`) || [];
    const { firstChild } = document.head;
    Array.from(styles).forEach((style) => {
      style[CSS_IN_JS_INSTANCE] = style[CSS_IN_JS_INSTANCE] || cssinjsInstanceId;
      if (style[CSS_IN_JS_INSTANCE] === cssinjsInstanceId)
        document.head.insertBefore(style, firstChild);
    });
    const styleHash = {};
    Array.from(document.querySelectorAll(`style[${ATTR_MARK}]`)).forEach((style) => {
      var _a;
      const hash = style.getAttribute(ATTR_MARK);
      if (styleHash[hash]) {
        if (style[CSS_IN_JS_INSTANCE] === cssinjsInstanceId)
          (_a = style.parentNode) == null ? void 0 : _a.removeChild(style);
      } else {
        styleHash[hash] = true;
      }
    });
  }
  return new Entity(cssinjsInstanceId);
}
const StyleContextKey = Symbol("StyleContextKey");
function getCache() {
  var _a, _b, _c;
  const instance = getCurrentInstance();
  let cache;
  if (instance && instance.appContext) {
    const globalCache = (_c = (_b = (_a = instance.appContext) == null ? void 0 : _a.config) == null ? void 0 : _b.globalProperties) == null ? void 0 : _c.__ANTDV_CSSINJS_CACHE__;
    if (globalCache) {
      cache = globalCache;
    } else {
      cache = createCache();
      if (instance.appContext.config.globalProperties)
        instance.appContext.config.globalProperties.__ANTDV_CSSINJS_CACHE__ = cache;
    }
  } else {
    cache = createCache();
  }
  return cache;
}
const defaultStyleContext = {
  cache: createCache(),
  defaultCache: true,
  hashPriority: "low"
};
function useStyleInject() {
  const cache = getCache();
  return inject(StyleContextKey, shallowRef({ ...defaultStyleContext, cache }));
}
function useStyleProvider(props) {
  const parentContext = useStyleInject();
  const context = shallowRef({
    ...defaultStyleContext,
    cache: createCache()
  });
  watch(
    [() => unref(props), parentContext],
    () => {
      const mergedContext = {
        ...parentContext.value
      };
      const propsValue = unref(props);
      Object.keys(propsValue).forEach((key) => {
        const value = propsValue[key];
        if (propsValue[key] !== void 0)
          mergedContext[key] = value;
      });
      const { cache } = propsValue;
      mergedContext.cache = mergedContext.cache || createCache();
      mergedContext.defaultCache = !cache && parentContext.value.defaultCache;
      context.value = mergedContext;
    },
    { immediate: true }
  );
  provide(StyleContextKey, context);
  return context;
}
const StyleProvider = withInstall(defineComponent({
  name: "AStyleProvider",
  inheritAttrs: false,
  props: {
    autoClear: Boolean,
    mock: String,
    cache: {
      type: Object
    },
    hashPriority: {
      type: String
    },
    container: Object,
    ssrInline: Boolean,
    transformers: Array,
    linters: Array,
    defaultCache: {
      type: Boolean
    }
  },
  setup(props, { slots }) {
    useStyleProvider(props);
    return () => {
      var _a;
      return (_a = slots.default) == null ? void 0 : _a.call(slots);
    };
  }
}));
var StyleContext = {
  useStyleInject,
  useStyleProvider,
  StyleProvider
};

export { ATTR_CACHE_PATH, ATTR_MARK, ATTR_TOKEN, CSS_IN_JS_INSTANCE, StyleProvider, createCache, StyleContext as default, useStyleInject, useStyleProvider };
//# sourceMappingURL=StyleContext.mjs.map
