/**
@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{nativeShadow as t,nativeCssVariables as e,cssBuild as n}from"./style-settings.js";import{parse as r,stringify as o,types as s,StyleNode as i}from"./css-parse.js";import{MEDIA_MATCH as l}from"./common-regex.js";import{processUnscopedStyle as u,isUnscopedStyle as c}from"./unscoped-style-handler.js";export function toCssText(t,n){return t?("string"==typeof t&&(t=r(t)),n&&forEachRule(t,n),o(t,e)):""}export function rulesForStyle(t){return!t.__cssRules&&t.textContent&&(t.__cssRules=r(t.textContent)),t.__cssRules||null}export function isKeyframesSelector(t){return Boolean(t.parent)&&t.parent.type===s.KEYFRAMES_RULE}export function forEachRule(t,e,n,r){if(!t)return;let o=!1,i=t.type;if(r&&i===s.MEDIA_RULE){let e=t.selector.match(l);e&&(window.matchMedia(e[1]).matches||(o=!0))}i===s.STYLE_RULE?e(t):n&&i===s.KEYFRAMES_RULE?n(t):i===s.MIXIN_RULE&&(o=!0);let u=t.rules;if(u&&!o)for(let t,o=0,s=u.length;o<s&&(t=u[o]);o++)forEachRule(t,e,n,r)}export function applyCss(t,e,n,r){let o=createScopeStyle(t,e);return applyStyle(o,n,r),o}export function createScopeStyle(t,e){let n=document.createElement("style");return e&&n.setAttribute("scope",e),n.textContent=t,n}let a=null;export function applyStylePlaceHolder(t){let e=document.createComment(" Shady DOM styles for "+t+" "),n=a?a.nextSibling:null,r=document.head;return r.insertBefore(e,n||r.firstChild),a=e,e}export function applyStyle(t,e,n){e=e||document.head;let r=n&&n.nextSibling||e.firstChild;if(e.insertBefore(t,r),a){t.compareDocumentPosition(a)===Node.DOCUMENT_POSITION_PRECEDING&&(a=t)}else a=t}export function isTargetedBuild(e){return t?"shadow"===e:"shady"===e}export function findMatchingParen(t,e){let n=0;for(let r=e,o=t.length;r<o;r++)if("("===t[r])n++;else if(")"===t[r]&&0==--n)return r;return-1}export function processVariableAndFallback(t,e){let n=t.indexOf("var(");if(-1===n)return e(t,"","","");let r=findMatchingParen(t,n+3),o=t.substring(n+4,r),s=t.substring(0,n),i=processVariableAndFallback(t.substring(r+1),e),l=o.indexOf(",");return-1===l?e(s,o.trim(),"",i):e(s,o.substring(0,l).trim(),o.substring(l+1).trim(),i)}export function setElementClassRaw(e,n){t?e.setAttribute("class",n):window.ShadyDOM.nativeMethods.setAttribute.call(e,"class",n)}export const wrap=window.ShadyDOM&&window.ShadyDOM.wrap||(t=>t);export function getIsExtends(t){let e=t.localName,n="",r="";return e?e.indexOf("-")>-1?n=e:(r=e,n=t.getAttribute&&t.getAttribute("is")||""):(n=t.is,r=t.extends),{is:n,typeExtension:r}}export function gatherStyleText(e){const n=[],r=e.querySelectorAll("style");for(let e=0;e<r.length;e++){const o=r[e];c(o)?t||(u(o),o.parentNode.removeChild(o)):(n.push(o.textContent),o.parentNode.removeChild(o))}return n.join("").trim()}export function splitSelectorList(t){const e=[];let n="";for(let r=0;r>=0&&r<t.length;r++)if("("===t[r]){const e=findMatchingParen(t,r);n+=t.slice(r,e+1),r=e}else","===t[r]?(e.push(n),n=""):n+=t[r];return n&&e.push(n),e}const f="css-build";export function getCssBuild(t){if(void 0!==n)return n;if(void 0===t.__cssBuild){const e=t.getAttribute(f);if(e)t.__cssBuild=e;else{const e=getBuildComment(t);""!==e&&function removeBuildComment(t){const e="template"===t.localName?t.content.firstChild:t.firstChild;e.parentNode.removeChild(e)}(t),t.__cssBuild=e}}return t.__cssBuild||""}export function elementHasBuiltCss(t){return""!==getCssBuild(t)}export function getBuildComment(t){const e="template"===t.localName?t.content.firstChild:t.firstChild;if(e instanceof Comment){const t=e.textContent.trim().split(":");if(t[0]===f)return t[1]}return""}export function isOptimalCssBuild(n=""){return!(""===n||!e)&&(t?"shadow"===n:"shady"===n)}