var e=require("fs"),r=require("path"),t=require("babel-plugin-macros");function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n=/*#__PURE__*/a(e),i=/*#__PURE__*/a(r),o=t.createMacro(function(e){var r=e.state,t=e.babel,a={value:0};e.references.default.forEach(function(e){var o,c,f;if("CallExpression"!==(null==(o=e.parentPath)?void 0:o.type))throw new Error("This is not supported: `"+(null!=(c=null==(f=e.findParent(function(e){return t.types.isExpression(e.node)}))?void 0:f.getSource())?c:"unknown")+"`. Please see the raw.macro documentation");!function(e){var r,t,a=e.referencePath,o=e.usageCounter,c=e.babel.types,f=a.parentPath,d=i.default.dirname(e.state.file.opts.filename),v=f.get("arguments"),h=v[0];switch(t=2==v.length&&"StringLiteral"===h.node.type?v[1].node.value:"utf-8",h.node.type){case"TemplateLiteral":var p=h.node,y=p.expressions,m=p.quasis;if(0===y.length){r=m[0].value.raw;break}try{if(void 0!==(r=f.get("arguments")[0].evaluate().value))break}catch(e){}if(""===m[0].value.raw)throw new Error("Invalid value, variable interpolation can't be at the start of template literal");if(y.length>2)throw new Error("Invalid value. You can only have 2 dynamic values max. 1 for directory name, 1 for file name.");var g=l+o.value;o.value++;var w=y.map(function(e,r){var t=r!==m.length-1&&h.node.quasis[r+1].value.raw.startsWith("/");return{name:e.name,isDirectory:t}}),b=i.default.join(d,m[0].value.raw),E=n.default.readdirSync(b).filter(function(e){try{return n.default.lstatSync(i.default.join(b,e)).isDirectory()===w[0].isDirectory}catch(e){return!1}}).flatMap(function(e){return w[0].isDirectory?e:e.endsWith(m[1].value.raw)?e.replace(m[1].value.raw,""):[]});return m.slice(1).forEach(function(e,r){w[r].isDirectory&&E.forEach(function(t,a){if(!e.tail){var o=i.default.join(b,t),l=n.default.readdirSync(o).filter(function(e){return e.endsWith(m[r+2].value.raw)});E[a]=[t,l]}})}),s(a).unshiftContainer("body",c.variableDeclaration("var",[c.variableDeclarator(c.identifier(g),u(c,E,b,m[m.length-1].value.raw,t))])),void f.replaceWith(c.expressionStatement(w.reduce(function(e,r){return c.memberExpression(e,c.identifier(r.name),!0)},c.identifier(g))));case"StringLiteral":r=h.node.value}if(void 0===r)throw new Error("There was a problem evaluating the value of the argument for the code: "+f.getSource()+". If the value is dynamic, please make sure that its value is statically deterministic.");var S=require.resolve(r,{paths:[d]}),P=n.default.readFileSync(S,{encoding:t});f.replaceWith(c.expressionStatement(c.stringLiteral(P)))}({referencePath:e,state:r,babel:t,usageCounter:a})})}),l="__raw_dynamic__";function u(e,r,t,a,o){void 0===a&&(a=""),void 0===o&&(o="utf-8");var l=r.map(function(r){if(Array.isArray(r))return e.objectProperty(e.stringLiteral(r[0]),u(e,r[1],i.default.join(t,r[0]),a,o));try{var l=a.startsWith(".")&&r.endsWith(a)?r:r+a,s=require.resolve("./"+l,{paths:[t]});return e.objectProperty(e.stringLiteral(r.replace(a,"")),e.stringLiteral(n.default.readFileSync(s,{encoding:o})))}catch(e){return null}}).filter(Boolean);if(0===l.length)throw new Error("Cannot resolve file "+a+" in these directories: "+r.join(", "));return e.objectExpression(l)}function s(e){return e.parentPath?s(e.parentPath):e}module.exports=o; //# sourceMappingURL=raw.macro.cjs.map