/**
 * Minified by jsDelivr using Terser v5.48.0.
 * Original file: /npm/webidl2js@21.2.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("node:path"),fs=require("node:fs/promises"),webidl=require("webidl2"),{format:format}=require("oxfmt"),Context=require("./context"),Types=require("./types"),Typedef=require("./constructs/typedef"),Interface=require("./constructs/interface"),Namespace=require("./constructs/namespace"),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,namespaces:a,interfaceMixins:r,callbackInterfaces:n,callbackFunctions:c,dictionaries:l,enumerations:o,typedefs:p}=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"namespace":if(t.partial)break;s=new Namespace(this.ctx,t,{implDir:e.impl}),a.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),n.set(s.name,s);break;case"callback":s=new CallbackFunction(this.ctx,t),c.set(s.name,s);break;case"includes":break;case"dictionary":if(t.partial)break;s=new Dictionary(this.ctx,t),l.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),p.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"namespace":if(!t.partial)break;if(this.ctx.options.suppressErrors&&!a.has(t.name))break;e=a.get(t.name).idl.members,e.push(...t.members),s=a.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&&!l.has(t.name))break;e=l.get(t.name).idl.members,e.push(...t.members),s=l.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)}}}_validateObservableArrays(){const e=(t,s)=>{if(Array.isArray(t.idlType))for(const e of t.idlType)Types.validateObservableArrayType(this.ctx,e);else if(t.idlType){const e="typedef"===t.type||"attribute"===t.type&&"static"!==t.special&&("interface"===s||"interface mixin"===s);Types.validateObservableArrayType(this.ctx,t.idlType,e)}for(const i of t.members||t.arguments||[])e(i,s)};for(const t of[this.ctx.interfaces,this.ctx.interfaceMixins,this.ctx.namespaces,this.ctx.dictionaries,this.ctx.callbackFunctions,this.ctx.callbackInterfaces])for(const s of t.values())e(s.idl,s.type)}async _writeFiles(e){const t=await fs.readFile(path.resolve(__dirname,"output/utils.js"));await fs.writeFile(this.utilPath,t);const{interfaces:s,namespaces:i,callbackInterfaces:a,callbackFunctions:r,builtinCallbackFunctions:n,dictionaries:c,enumerations:l}=this.ctx;let o=path.relative(e,this.utilPath).replaceAll("\\","/");"."!==o[0]&&(o=`./${o}`),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("${o}");\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().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("${o}");\n        ${s}\n        const Impl = require("${i}.js");\n      `,s=await this._prettify(s),await fs.writeFile(path.join(e,`${t.name}.js`),s)}));const p=async t=>{let s=t.toString();s=`\n        "use strict";\n\n        const conversions = require("webidl-conversions");\n        const utils = require("${o}");\n        ${s}\n      `,s=await this._prettify(s),await fs.writeFile(path.join(e,`${t.name}.js`),s)},u=[...a.values(),...r.values().filter(e=>n.get(e.name)!==e),...c.values()];await Promise.all(u.map(p));const m=[...s.values(),...i.values(),...u];await Promise.all(n.values().filter(e=>r.get(e.name)===e&&m.some(t=>t.requires.has(e.name))).map(p)),await Promise.all(l.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);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),this._validateObservableArrays(),await this._writeFiles(e)}}module.exports=Transformer;
//# sourceMappingURL=/sm/d06b65684993a187c07b0f0ad7461c973509a6a299de0c778522cdb6684b1ca3.map