'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
var Cache = require('./Cache.js');
var util = require('./util.js');

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 Cache.default();
}
const StyleContextKey = Symbol("StyleContextKey");
const defaultStyleContext = {
  cache: createCache(),
  defaultCache: true,
  hashPriority: "low"
};
const useStyleInject = () => {
  return vue.inject(StyleContextKey, vue.shallowRef({ ...defaultStyleContext }));
};
const useStyleProvider = (props) => {
  const parentContext = useStyleInject();
  const context = vue.shallowRef({ ...defaultStyleContext });
  vue.watch(
    [props, parentContext],
    () => {
      const mergedContext = {
        ...parentContext.value
      };
      const propsValue = vue.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 }
  );
  vue.provide(StyleContextKey, context);
  return context;
};
const StyleProvider = util.withInstall(vue.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
};

exports.ATTR_DEV_CACHE_PATH = ATTR_DEV_CACHE_PATH;
exports.ATTR_MARK = ATTR_MARK;
exports.ATTR_TOKEN = ATTR_TOKEN;
exports.CSS_IN_JS_INSTANCE = CSS_IN_JS_INSTANCE;
exports.CSS_IN_JS_INSTANCE_ID = CSS_IN_JS_INSTANCE_ID;
exports.StyleProvider = StyleProvider;
exports.createCache = createCache;
exports.default = StyleContext;
exports.useStyleInject = useStyleInject;
exports.useStyleProvider = useStyleProvider;
//# sourceMappingURL=StyleContext.js.map
