namespace XFESystem.Browser;
internal static class BrowserDomCollections
{
internal const string Source = "\n" + """
const collectionStates=new WeakMap();
function collectionSource(value){const state=collectionStates.get(value);if(!state)throw new TypeError('Illegal collection invocation');return state.source;}
function collectionItem(index){const source=collectionSource(this);index=Number(index)>>>0;return index<source.length()?source.item(index):null;}
class NodeList {
constructor(){throw new TypeError('Illegal constructor');}get length(){return collectionSource(this).length();}item(index){return collectionItem.call(this,index);}
forEach(callback,thisArg){return Array.prototype.forEach.call(this,callback,thisArg);}entries(){return Array.prototype.entries.call(this);}keys(){return Array.prototype.keys.call(this);}values(){return Array.prototype.values.call(this);}[Symbol.iterator](){return this.values();}
}
class HTMLCollection {
constructor(){throw new TypeError('Illegal constructor');}get length(){return collectionSource(this).length();}item(index){return collectionItem.call(this,index);}
namedItem(name){name=String(name);if(!name)return null;return collectionSource(this).all().find(node=>node.id===name||node.getAttribute?.('name')===name)??null;}
[Symbol.iterator](){return Array.prototype.values.call(this);}
}
class CSSRuleList {
constructor(){throw new TypeError('Illegal constructor');}get length(){return collectionSource(this).length();}item(index){return collectionItem.call(this,index);}
[Symbol.iterator](){return Array.prototype.values.call(this);}
}
for(const constructor of [NodeList,HTMLCollection,CSSRuleList])Object.defineProperty(constructor.prototype,Symbol.toStringTag,{value:constructor.name,configurable:true});
Object.assign(window,{NodeList,HTMLCollection,CSSRuleList});
function liveList(source,kind='NodeList'){
if(typeof source==='function'){const readAll=source;source={all:readAll,length(){return readAll().length;},item(index){return readAll()[index]??null;}};}
const constructor=kind==='HTMLCollection'?HTMLCollection:kind==='CSSRuleList'?CSSRuleList:NodeList;
const target=Object.create(constructor.prototype),index=name=>typeof name==='string'&&/^(0|[1-9]\d*)$/.test(name);
// Jint's TryGetValue evaluates inherited getters with the
// prototype as receiver. Use the same own-accessor projection as
// other DOM properties so generic Array operations see this list.
Object.defineProperty(target,'length',{get(){return source.length();},configurable:true});
function named(name){return kind==='HTMLCollection'&&typeof name==='string'&&!Reflect.has(target,name)?source.all().find(node=>node.id===name||node.getAttribute?.('name')===name):undefined;}
const proxy=new Proxy(target,{
get(target,name,receiver){if(index(name))return +name<source.length()?source.item(+name):undefined;return Reflect.has(target,name)?Reflect.get(target,name,receiver):named(name);},
has(target,name){return index(name)?+name<source.length():Reflect.has(target,name)||named(name)!==undefined;},
ownKeys(target){const keys=Array.from({length:source.length()},(_,i)=>String(i));if(kind==='HTMLCollection')for(const node of source.all())for(const name of [node.id,node.getAttribute?.('name')])if(name&&!index(name)&&!Reflect.has(target,name)&&!keys.includes(name))keys.push(name);return keys.concat(Reflect.ownKeys(target));},
getOwnPropertyDescriptor(target,name){if(index(name))return +name<source.length()?{value:source.item(+name),writable:false,enumerable:true,configurable:true}:undefined;const value=named(name);return value!==undefined?{value,writable:false,enumerable:false,configurable:true}:Reflect.getOwnPropertyDescriptor(target,name);},
set(target,name,value,receiver){if(index(name)||name==='length'||named(name)!==undefined)return false;return Reflect.set(target,name,value,receiver);},
defineProperty(target,name,descriptor){if(index(name)||name==='length'||named(name)!==undefined)return false;return Reflect.defineProperty(target,name,descriptor);},
deleteProperty(target,name){if(index(name)||named(name)!==undefined)return false;return Reflect.deleteProperty(target,name);}
});collectionStates.set(proxy,{source,kind});return proxy;
}
function nodeList(values){return liveList({all(){return values;},length(){return values.length;},item(index){return values[index]??null;}});}
""" + "\n";
}
namespace XFESystem.Browser;
internal static class BrowserDomCollections
{
internal const string Source = "\n" + """
const collectionStates=new WeakMap();
function collectionSource(value){const state=collectionStates.get(value);if(!state)throw new TypeError('Illegal collection invocation');return state.source;}
function collectionItem(index){const source=collectionSource(this);index=Number(index)>>>0;return index<source.length()?source.item(index):null;}
class NodeList {
constructor(){throw new TypeError('Illegal constructor');}get length(){return collectionSource(this).length();}item(index){return collectionItem.call(this,index);}
forEach(callback,thisArg){return Array.prototype.forEach.call(this,callback,thisArg);}entries(){return Array.prototype.entries.call(this);}keys(){return Array.prototype.keys.call(this);}values(){return Array.prototype.values.call(this);}[Symbol.iterator](){return this.values();}
}
class HTMLCollection {
constructor(){throw new TypeError('Illegal constructor');}get length(){return collectionSource(this).length();}item(index){return collectionItem.call(this,index);}
namedItem(name){name=String(name);if(!name)return null;return collectionSource(this).all().find(node=>node.id===name||node.getAttribute?.('name')===name)??null;}
[Symbol.iterator](){return Array.prototype.values.call(this);}
}
class CSSRuleList {
constructor(){throw new TypeError('Illegal constructor');}get length(){return collectionSource(this).length();}item(index){return collectionItem.call(this,index);}
[Symbol.iterator](){return Array.prototype.values.call(this);}
}
for(const constructor of [NodeList,HTMLCollection,CSSRuleList])Object.defineProperty(constructor.prototype,Symbol.toStringTag,{value:constructor.name,configurable:true});
Object.assign(window,{NodeList,HTMLCollection,CSSRuleList});
function liveList(source,kind='NodeList'){
if(typeof source==='function'){const readAll=source;source={all:readAll,length(){return readAll().length;},item(index){return readAll()[index]??null;}};}
const constructor=kind==='HTMLCollection'?HTMLCollection:kind==='CSSRuleList'?CSSRuleList:NodeList;
const target=Object.create(constructor.prototype),index=name=>typeof name==='string'&&/^(0|[1-9]\d*)$/.test(name);
// Jint's TryGetValue evaluates inherited getters with the
// prototype as receiver. Use the same own-accessor projection as
// other DOM properties so generic Array operations see this list.
Object.defineProperty(target,'length',{get(){return source.length();},configurable:true});
function named(name){return kind==='HTMLCollection'&&typeof name==='string'&&!Reflect.has(target,name)?source.all().find(node=>node.id===name||node.getAttribute?.('name')===name):undefined;}
const proxy=new Proxy(target,{
get(target,name,receiver){if(index(name))return +name<source.length()?source.item(+name):undefined;return Reflect.has(target,name)?Reflect.get(target,name,receiver):named(name);},
has(target,name){return index(name)?+name<source.length():Reflect.has(target,name)||named(name)!==undefined;},
ownKeys(target){const keys=Array.from({length:source.length()},(_,i)=>String(i));if(kind==='HTMLCollection')for(const node of source.all())for(const name of [node.id,node.getAttribute?.('name')])if(name&&!index(name)&&!Reflect.has(target,name)&&!keys.includes(name))keys.push(name);return keys.concat(Reflect.ownKeys(target));},
getOwnPropertyDescriptor(target,name){if(index(name))return +name<source.length()?{value:source.item(+name),writable:false,enumerable:true,configurable:true}:undefined;const value=named(name);return value!==undefined?{value,writable:false,enumerable:false,configurable:true}:Reflect.getOwnPropertyDescriptor(target,name);},
set(target,name,value,receiver){if(index(name)||name==='length'||named(name)!==undefined)return false;return Reflect.set(target,name,value,receiver);},
defineProperty(target,name,descriptor){if(index(name)||name==='length'||named(name)!==undefined)return false;return Reflect.defineProperty(target,name,descriptor);},
deleteProperty(target,name){if(index(name)||named(name)!==undefined)return false;return Reflect.deleteProperty(target,name);}
});collectionStates.set(proxy,{source,kind});return proxy;
}
function nodeList(values){return liveList({all(){return values;},length(){return values.length;},item(index){return values[index]??null;}});}
""" + "\n";
}