import { 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_DEV_CACHE_PATH = "data-dev-cache-path";
const CSS_IN_JS_INSTANCE = "__cssinjs_instance__";
const CSS_IN_JS_INSTANCE_ID = Math.random().toString(12).slice(2);
function createCache() {
  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] || CSS_IN_JS_INSTANCE_ID;
      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] === CSS_IN_JS_INSTANCE_ID)
          (_a = style.parentNode) == null ? void 0 : _a.removeChild(style);
      } else {
        styleHash[hash] = true;
      }
    });
  }
  return new Entity();
}
const StyleContextKey = Symbol("StyleContextKey");
const defaultStyleContext = {
  cache: createCache(),
  defaultCache: true,
  hashPriority: "low"
};
const useStyleInject = () => {
  return inject(StyleContextKey, shallowRef({ ...defaultStyleContext }));
};
const useStyleProvider = (props) => {
  const parentContext = useStyleInject();
  const context = shallowRef({ ...defaultStyleContext });
  watch(
    [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,
      default: () => createCache()
    },
    hashPriority: {
      type: String,
      default: "low"
    },
    container: Object,
    ssrInline: Boolean,
    transformers: Array,
    linters: Array,
    defaultCache: {
      type: Boolean,
      default: true
    }
  },
  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_DEV_CACHE_PATH, ATTR_MARK, ATTR_TOKEN, CSS_IN_JS_INSTANCE, CSS_IN_JS_INSTANCE_ID, StyleProvider, createCache, StyleContext as default, useStyleInject, useStyleProvider };
//# sourceMappingURL=StyleContext.mjs.map
