import { configExtension } from "lexical";
class ExtensionsList {
    add(extension) {
        this.items.push(extension);
        return this;
    }
    remove(extension) {
        const targetName = extension.name;
        this.items = this.items.filter((item)=>extensionName(item) !== targetName);
        return this;
    }
    replace(oldExtension, newExtension) {
        const targetName = oldExtension.name;
        let replaced = false;
        this.items = this.items.map((item)=>{
            if (!replaced && extensionName(item) === targetName) {
                replaced = true;
                return newExtension;
            }
            return item;
        });
        if (!replaced) this.items.push(newExtension);
        return this;
    }
    configure(extension, config) {
        const wrapped = configExtension(extension, config);
        return this.replace(extension, wrapped);
    }
    has(extension) {
        const targetName = extension.name;
        return this.items.some((item)=>extensionName(item) === targetName);
    }
    clone() {
        return new ExtensionsList(this.items);
    }
    toArray() {
        return [
            ...this.items
        ];
    }
    constructor(items = []){
        this.items = [
            ...items
        ];
    }
}
function extensionName(item) {
    if (Array.isArray(item)) {
        const head = item[0];
        if (null != head && 'object' == typeof head && 'name' in head) return head.name;
        return;
    }
    if (null != item && 'object' == typeof item && 'name' in item) return item.name;
}
export { ExtensionsList };
