import S from"../../_util/canUseDom.mjs";import g from"./contains.mjs";const d="data-vc-order",C="vc-util-key",s=new Map;function a({mark:e}={}){return e?e.startsWith("data-")?e:`data-${e}`:C}function o(e){return e.attachTo?e.attachTo:document.querySelector("head")||document.body}function y(e){return e==="queue"?"prependQueue":e?"prepend":"append"}function f(e){return Array.from((s.get(e)||e).children).filter(r=>r.tagName==="STYLE")}function l(e,r={}){if(!S())return null;const{csp:n,prepend:c}=r,t=document.createElement("style");t.setAttribute(d,y(c)),n?.nonce&&(t.nonce=n?.nonce),t.innerHTML=e;const i=o(r),{firstChild:m}=i;if(c){if(c==="queue"){const u=f(i).filter(p=>["prepend","prependQueue"].includes(p.getAttribute(d)));if(u.length)return i.insertBefore(t,u[u.length-1].nextSibling),t}i.insertBefore(t,m)}else i.appendChild(t);return t}function h(e,r={}){const n=o(r);return f(n).find(c=>c.getAttribute(a(r))===e)}function x(e,r={}){const n=h(e,r);n&&o(r).removeChild(n)}function N(e,r){const n=s.get(e);if(!n||!g(document,n)){const c=l("",r),{parentNode:t}=c;s.set(e,t),e.removeChild(c)}}function E(e,r,n={}){const c=o(n);N(c,n);const t=h(r,n);if(t)return n.csp?.nonce&&t.nonce!==n.csp?.nonce&&(t.nonce=n.csp?.nonce),t.innerHTML!==e&&(t.innerHTML=e),t;const i=l(e,n);return i.setAttribute(a(n),r),i}export{l as injectCSS,x as removeCSS,E as updateCSS};
