/**
@license
Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
import{forEachRule as t,processVariableAndFallback as e,rulesForStyle as r,toCssText as s,gatherStyleText as i}from"./style-util.js";import{MIXIN_MATCH as n,VAR_ASSIGN as l}from"./common-regex.js";import{detectMixin as o}from"./common-utils.js";import{StyleNode as p}from"./css-parse.js";const a=/;\s*/m,m=/^\s*(initial)|(inherit)\s*$/,h=/\s*!important/,u="_-_";class MixinMap{constructor(){this._map={}}set(t,e){t=t.trim(),this._map[t]={properties:e,dependants:{}}}get(t){return t=t.trim(),this._map[t]||null}}let c=null;class ApplyShim{constructor(){this._currentElement=null,this._measureElement=null,this._map=new MixinMap}detectMixin(t){return o(t)}gatherStyles(t){const e=i(t.content);if(e){const r=document.createElement("style");return r.textContent=e,t.content.insertBefore(r,t.content.firstChild),r}return null}transformTemplate(t,e){void 0===t._gatheredStyle&&(t._gatheredStyle=this.gatherStyles(t));const r=t._gatheredStyle;return r?this.transformStyle(r,e):null}transformStyle(t,e=""){let i=r(t);return this.transformRules(i,e),t.textContent=s(i),i}transformCustomStyle(e){let i=r(e);return t(i,(t=>{":root"===t.selector&&(t.selector="html"),this.transformRule(t)})),e.textContent=s(i),i}transformRules(e,r){this._currentElement=r,t(e,(t=>{this.transformRule(t)})),this._currentElement=null}transformRule(t){t.cssText=this.transformCssText(t.parsedCssText,t),":root"===t.selector&&(t.selector=":host > *")}transformCssText(t,e){return t=t.replace(l,((t,r,s,i)=>this._produceCssProperties(t,r,s,i,e))),this._consumeCssProperties(t,e)}_getInitialValueForProperty(t){return this._measureElement||(this._measureElement=document.createElement("meta"),this._measureElement.setAttribute("apply-shim-measure",""),this._measureElement.style.all="initial",document.head.appendChild(this._measureElement)),window.getComputedStyle(this._measureElement).getPropertyValue(t)}_fallbacksFromPreviousRules(e){let r=e;for(;r.parent;)r=r.parent;const s={};let i=!1;return t(r,(t=>{i=i||t===e,i||t.selector===e.selector&&Object.assign(s,this._cssTextToMap(t.parsedCssText))})),s}_consumeCssProperties(t,e){let r=null;for(;r=n.exec(t);){let s=r[0],i=r[1],l=r.index,o=l+s.indexOf("@apply"),p=l+s.length,a=t.slice(0,o),m=t.slice(p),h=e?this._fallbacksFromPreviousRules(e):{};Object.assign(h,this._cssTextToMap(a));let u=this._atApplyToCssProperties(i,h);t=`${a}${u}${m}`,n.lastIndex=l+u.length}return t}_atApplyToCssProperties(t,e){t=t.replace(a,"");let r=[],s=this._map.get(t);if(s||(this._map.set(t,{}),s=this._map.get(t)),s){let i,n,l;this._currentElement&&(s.dependants[this._currentElement]=!0);const o=s.properties;for(i in o)l=e&&e[i],n=[i,": var(",t,u,i],l&&n.push(",",l.replace(h,"")),n.push(")"),h.test(o[i])&&n.push(" !important"),r.push(n.join(""))}return r.join("; ")}_replaceInitialOrInherit(t,e){let r=m.exec(e);return r&&(e=r[1]?this._getInitialValueForProperty(t):"apply-shim-inherit"),e}_cssTextToMap(t,e=!1){let r,s,i=t.split(";"),n={};for(let t,l,o=0;o<i.length;o++)t=i[o],t&&(l=t.split(":"),l.length>1&&(r=l[0].trim(),s=l.slice(1).join(":"),e&&(s=this._replaceInitialOrInherit(r,s)),n[r]=s));return n}_invalidateMixinEntry(t){if(c)for(let e in t.dependants)e!==this._currentElement&&c(e)}_produceCssProperties(t,r,s,i,n){if(s&&e(s,((t,e)=>{e&&this._map.get(e)&&(i=`@apply ${e};`)})),!i)return t;let l=this._consumeCssProperties(""+i,n),o=t.slice(0,t.indexOf("--")),p=this._cssTextToMap(l,!0),a=p,m=this._map.get(r),h=m&&m.properties;h?a=Object.assign(Object.create(h),p):this._map.set(r,a);let c,y,_=[],f=!1;for(c in a)y=p[c],void 0===y&&(y="initial"),h&&!(c in h)&&(f=!0),_.push(`${r}${u}${c}: ${y}`);return f&&this._invalidateMixinEntry(m),m&&(m.properties=a),s&&(o=`${t};${o}`),`${o}${_.join("; ")};`}}ApplyShim.prototype.detectMixin=ApplyShim.prototype.detectMixin,ApplyShim.prototype.transformStyle=ApplyShim.prototype.transformStyle,ApplyShim.prototype.transformCustomStyle=ApplyShim.prototype.transformCustomStyle,ApplyShim.prototype.transformRules=ApplyShim.prototype.transformRules,ApplyShim.prototype.transformRule=ApplyShim.prototype.transformRule,ApplyShim.prototype.transformTemplate=ApplyShim.prototype.transformTemplate,ApplyShim.prototype._separator=u,Object.defineProperty(ApplyShim.prototype,"invalidCallback",{get:()=>c,set(t){c=t}});export default ApplyShim;