"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.extract = exports.default = exports.CSS_VAR_PREFIX = void 0;
var _dynamicCSS = require("@rc-component/util/lib/Dom/dynamicCSS");
var _react = require("react");
var _StyleContext = _interopRequireWildcard(require("../StyleContext"));
var _util = require("../util");
var _cssVariables = require("../util/css-variables");
var _useGlobalCache = _interopRequireDefault(require("./useGlobalCache"));
var _useStyleRegister = require("./useStyleRegister");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
const CSS_VAR_PREFIX = exports.CSS_VAR_PREFIX = 'cssVar';
const useCSSVarRegister = (config, fn) => {
  const {
    key,
    prefix,
    unitless,
    ignore,
    token,
    hashId,
    scope,
    nonce
  } = config;
  const {
    cache: {
      instanceId
    },
    container,
    hashPriority
  } = (0, _react.useContext)(_StyleContext.default);
  const {
    _tokenKey: tokenKey
  } = token;
  const scopeKey = Array.isArray(scope) ? scope.join('@@') : scope;
  const stylePath = [...config.path, key, scopeKey, tokenKey];
  const cache = (0, _useGlobalCache.default)(CSS_VAR_PREFIX, stylePath, () => {
    const originToken = fn();
    const [mergedToken, cssVarsStr] = (0, _cssVariables.transformToken)(originToken, key, {
      prefix,
      unitless,
      ignore,
      scope,
      hashPriority,
      hashCls: hashId
    });
    const styleId = (0, _useStyleRegister.uniqueHash)(stylePath, cssVarsStr);
    return [mergedToken, cssVarsStr, styleId, key];
  }, ([,, styleId]) => {
    if (_util.isClientSide) {
      (0, _dynamicCSS.removeCSS)(styleId, {
        mark: _StyleContext.ATTR_MARK,
        attachTo: container
      });
    }
  }, ([, cssVarsStr, styleId]) => {
    if (!cssVarsStr) {
      return;
    }
    let mergedCSSConfig = {
      mark: _StyleContext.ATTR_MARK,
      prepend: 'queue',
      attachTo: container,
      priority: -999
    };
    mergedCSSConfig = (0, _util.injectCSPNonce)(mergedCSSConfig, nonce);
    const style = (0, _dynamicCSS.updateCSS)(cssVarsStr, styleId, mergedCSSConfig);
    style[_StyleContext.CSS_IN_JS_INSTANCE] = instanceId;

    // Used for `useCacheToken` to remove on batch when token removed
    style.setAttribute(_StyleContext.ATTR_TOKEN, key);
  });
  return cache;
};
const extract = (cache, effectStyles, options) => {
  const [, styleStr, styleId, cssVarKey] = cache;
  const {
    plain
  } = options || {};
  if (!styleStr) {
    return null;
  }
  const order = -999;

  // ====================== Style ======================
  // Used for @rc-component/util
  const sharedAttrs = {
    'data-rc-order': 'prependQueue',
    'data-rc-priority': `${order}`
  };
  const styleText = (0, _util.toStyleStr)(styleStr, cssVarKey, styleId, sharedAttrs, plain);
  return [order, styleId, styleText];
};
exports.extract = extract;
var _default = exports.default = useCSSVarRegister;