/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/webidl2js@20.0.0/lib/transformer.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
"use strict";const path=require("path"),fs=require("fs/promises"),webidl=require("webidl2"),{format:format}=require("oxfmt"),Context=require("./context"),Typedef=require("./constructs/typedef"),Interface=require("./constructs/interface"),InterfaceMixin=require("./constructs/interface-mixin"),CallbackInterface=require("./constructs/callback-interface.js"),CallbackFunction=require("./constructs/callback-function"),Dictionary=require("./constructs/dictionary"),Enumeration=require("./constructs/enumeration");class Transformer{constructor(e={}){this.ctx=new Context({implSuffix:e.implSuffix,processCEReactions:e.processCEReactions,processHTMLConstructor:e.processHTMLConstructor,processReflect:e.processReflect,options:{suppressErrors:Boolean(e.suppressErrors)}}),this.sources=[],this.utilPath=null}addSource(e,t){if("string"!=typeof e)throw new TypeError("idl path has to be a string");if("string"!=typeof t)throw new TypeError("impl path has to be a string");return this.sources.push({idlPath:path.resolve(e),impl:path.resolve(t)}),this}async _collectSources(){const e=await Promise.all(this.sources.map((e=>fs.stat(e.idlPath)))),t=await Promise.all(e.map(((e,t)=>e.isDirectory()?fs.readdir(this.sources[t].idlPath):null))),s=[];for(let i=0;i<e.length;++i)if(t[i])for(const e of t[i])e.endsWith(".webidl")&&s.push({idlPath:path.join(this.sources[i].idlPath,e),impl:this.sources[i].impl});else s.push({idlPath:this.sources[i].idlPath,impl:this.sources[i].impl});return s}async _readFiles(e){const t=[],s=await Promise.all(e.map((e=>fs.readFile(e.idlPath,{encoding:"utf-8"}))));for(let i=0;i<e.length;++i)t.push({idlContent:s[i],impl:e[i].impl});return t}_parse(e,t){const s=t.map((e=>({idl:webidl.parse(e.idlContent),impl:e.impl})));this.ctx.initialize();const{interfaces:i,interfaceMixins:r,callbackInterfaces:a,callbackFunctions:n,dictionaries:c,enumerations:o,typedefs:l}=this.ctx;for(const e of s)for(const t of e.idl){let s;switch(t.type){case"interface":if(t.partial)break;s=new Interface(this.ctx,t,{implDir:e.impl}),i.set(s.name,s);break;case"interface mixin":if(t.partial)break;s=new InterfaceMixin(this.ctx,t),r.set(s.name,s);break;case"callback interface":s=new CallbackInterface(this.ctx,t),a.set(s.name,s);break;case"callback":s=new CallbackFunction(this.ctx,t),n.set(s.name,s);break;case"includes":break;case"dictionary":if(t.partial)break;s=new Dictionary(this.ctx,t),c.set(s.name,s);break;case"enum":s=new Enumeration(this.ctx,t),o.set(s.name,s);break;case"typedef":s=new Typedef(this.ctx,t),l.set(s.name,s);break;default:if(!this.ctx.options.suppressErrors)throw new Error(`Can't convert type '${t.type}'`)}}for(const e of s)for(const t of e.idl){let e,s;switch(t.type){case"interface":if(!t.partial)break;if(this.ctx.options.suppressErrors&&!i.has(t.name))break;e=i.get(t.name).idl.members,e.push(...t.members),s=i.get(t.name).idl.extAttrs,s.push(...t.extAttrs);break;case"interface mixin":if(!t.partial)break;if(this.ctx.options.suppressErrors&&!r.has(t.name))break;e=r.get(t.name).idl.members,e.push(...t.members),s=r.get(t.name).idl.extAttrs,s.push(...t.extAttrs);break;case"dictionary":if(!t.partial)break;if(this.ctx.options.suppressErrors&&!c.has(t.name))break;e=c.get(t.name).idl.members,e.push(...t.members),s=c.get(t.name).idl.extAttrs,s.push(...t.extAttrs);break;case"includes":if(this.ctx.options.suppressErrors&&!i.has(t.target))break;i.get(t.target).includes(t.includes)}}}async _writeFiles(e){const t=await fs.readFile(path.resolve(__dirname,"output/utils.js"));await fs.writeFile(this.utilPath,t);const{interfaces:s,callbackInterfaces:i,callbackFunctions:r,dictionaries:a,enumerations:n}=this.ctx;let c=path.relative(e,this.utilPath).replaceAll("\\","/");"."!==c[0]&&(c=`./${c}`),await Promise.all(s.values().map((async t=>{let s=t.toString(),i=path.relative(e,path.resolve(t.opts.implDir,t.name+this.ctx.implSuffix));i=i.replaceAll("\\","/"),"."!==i[0]&&(i=`./${i}`),s=`\n        "use strict";\n\n        const conversions = require("webidl-conversions");\n        const utils = require("${c}");\n        ${s}\n        const Impl = require("${i}.js");\n      `,s=await this._prettify(s),await fs.writeFile(path.join(e,`${t.name}.js`),s)}))),await Promise.all([...i.values(),...r.values(),...a.values()].map((async t=>{let s=t.toString();s=`\n          "use strict";\n\n          const conversions = require("webidl-conversions");\n          const utils = require("${c}");\n          ${s}\n        `,s=await this._prettify(s),await fs.writeFile(path.join(e,`${t.name}.js`),s)}))),await Promise.all(n.values().map((async t=>{const s=await this._prettify(`\n        "use strict";\n\n        ${t.toString()}\n      `);await fs.writeFile(path.join(e,`${t.name}.js`),s)})))}async _prettify(e){const{code:t}=await format("output.js",e,{printWidth:120,trailingComma:"none",arrowParens:"avoid"});return t}async generate(e){this.utilPath||(this.utilPath=path.join(e,"utils.js"));const t=await this._collectSources(),s=await this._readFiles(t);this._parse(e,s),await this._writeFiles(e)}}module.exports=Transformer;
//# sourceMappingURL=/sm/1b88673cfc023d138e32da9a66624bc2a3381332f5b0c009000665e67957f664.map