/**
@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{DomModule as t}from"../elements/dom-module.js";import{resolveCss as e}from"./resolve-url.js";const o="shady-unscoped";function importModule(e){return t.import(e)}function styleForImport(t){let o=t.body?t.body:t;const s=e(o.textContent,t.baseURI),r=document.createElement("style");return r.textContent=s,r}export function stylesFromModules(t){const e=t.trim().split(/\s+/),o=[];for(let t=0;t<e.length;t++)o.push(...stylesFromModule(e[t]));return o}export function stylesFromModule(t){const e=importModule(t);if(!e)return console.warn("Could not find style data in module named",t),[];if(void 0===e._styles){const t=[];t.push(..._stylesFromModuleImports(e));const o=e.querySelector("template");o&&t.push(...stylesFromTemplate(o,e.assetpath)),e._styles=t}return e._styles}export function stylesFromTemplate(t,o){if(!t._styles){const s=[],r=t.content.querySelectorAll("style");for(let t=0;t<r.length;t++){let l=r[t],n=l.getAttribute("include");n&&s.push(...stylesFromModules(n).filter((function(t,e,o){return o.indexOf(t)===e}))),o&&(l.textContent=e(l.textContent,o)),s.push(l)}t._styles=s}return t._styles}export function stylesFromModuleImports(t){let e=importModule(t);return e?_stylesFromModuleImports(e):[]}function _stylesFromModuleImports(t){const e=[],s=t.querySelectorAll("link[rel=import][type~=css]");for(let t=0;t<s.length;t++){let r=s[t];if(r.import){const t=r.import,s=r.hasAttribute(o);if(s&&!t._unscopedStyle){const e=styleForImport(t);e.setAttribute(o,""),t._unscopedStyle=e}else t._style||(t._style=styleForImport(t));e.push(s?t._unscopedStyle:t._style)}}return e}export function cssFromModules(t){let e=t.trim().split(/\s+/),o="";for(let t=0;t<e.length;t++)o+=cssFromModule(e[t]);return o}export function cssFromModule(t){let e=importModule(t);if(e&&void 0===e._cssText){let t=_cssFromModuleImports(e),o=e.querySelector("template");o&&(t+=cssFromTemplate(o,e.assetpath)),e._cssText=t||null}return e||console.warn("Could not find style data in module named",t),e&&e._cssText||""}export function cssFromTemplate(t,e){let o="";const s=stylesFromTemplate(t,e);for(let t=0;t<s.length;t++){let e=s[t];e.parentNode&&e.parentNode.removeChild(e),o+=e.textContent}return o}export function cssFromModuleImports(t){let e=importModule(t);return e?_cssFromModuleImports(e):""}function _cssFromModuleImports(t){let e="",o=_stylesFromModuleImports(t);for(let t=0;t<o.length;t++)e+=o[t].textContent;return e}