"use strict";var e=require("@csstools/css-tokenizer"),r=require("@csstools/css-parser-algorithms"),t=require("node:path");const s=/^([-_a-z0-9]+:)?\/\//i;function rebase(e,r,i){if(e.startsWith("data:"))return!1;if(s.test(e))return!1;if(e.startsWith("/"))return e;if(e.startsWith("#"))return e;try{const r=new URL(e);if(r.port||r.protocol)return!1}catch{}const o=t.posix.resolve(t.posix.join(r,e));return t.posix.relative(i,o)}function serializeString(e){let r="";for(const t of e){const e=t.codePointAt(0);if(void 0!==e)switch(e){case 0:r+=String.fromCodePoint(65533);break;case 127:r+=`\\${e.toString(16)}`;break;case 34:case 39:case 92:r+=`\\${t}`;break;default:if(1<=e&&e<=31){r+=`\\${e.toString(16)} `;break}r+=t}else r+=String.fromCodePoint(65533)}return r}function normalizedDir(e){return t.parse(t.resolve(e.trim())).dir.split(t.sep).join(t.posix.sep)}const i=/^initial-value$/i,o=/^property$/i,n=/^syntax$/i,a=/url\(/i,u=/^url$/i,l=//i,creator=()=>({postcssPlugin:"postcss-rebase-url",prepare(){const t=new WeakSet,s=new Set;return{postcssPlugin:"postcss-rebase-url",Once(e){e.walkAtRules(o,(e=>{if(!e.nodes)return;const r=e.nodes.find((e=>{if("decl"===e.type&&n.test(e.prop))return!0}));r&&l.test(r.value)&&s.add(e.params.trim())}))},Declaration(n,{result:l}){if(t.has(n))return;if(n.variable&&!s.has(n.prop))return;if(i.test(n.prop)&&"atrule"===n.parent?.type&&o.test(n.parent.name))return;const{from:c}=l.opts;if(!c)return;if(!n.source?.input.from)return;if(!a.test(n.value))return;const p=normalizedDir(c),f=n.source.input.from.trim();if(!f)return;const d=normalizedDir(f),v=r.parseCommaSeparatedListOfComponentValues(e.tokenize({css:n.value})),m=r.replaceComponentValues(v,(t=>{if(r.isTokenNode(t)&&e.isTokenURL(t.value)){const e=rebase(t.value[4].value.trim(),d,p);if(e)return t.value[4].value=e,t.value[1]=`url(${serializeString(e)})`,t}if(r.isFunctionNode(t)&&u.test(t.getName()))for(const s of t.value)if(!r.isWhitespaceNode(s)&&!r.isCommentNode(s)&&r.isTokenNode(s)&&e.isTokenString(s.value)){const e=rebase(s.value[4].value.trim(),d,p);if(e)return s.value[4].value=e,s.value[1]=`"${serializeString(e)}"`,t;break}})),g=r.stringify(m);g!==n.value&&(n.value=g,t.add(n))}}}});creator.postcss=!0,module.exports=creator;