export const LegacyUtil = {
    getDOM(source) {
        const parser = new DOMParser();
        var DOM = parser.parseFromString(source, 'application/xml');
        if (DOM.documentElement.nodeName !== 'parsererror') {
            return DOM.documentElement;
        }
    },
    transformBlock(root, selector, mutator) {
        const all = [...root.querySelectorAll(selector)];
        all.forEach(b => mutator(b));
    },
    transformField(block, name, mutator) {
        const field = block.querySelector(`field[name="${name}"]`);
        if (!field) {
            return;
        }
        const result = mutator(field.getAttribute('name'), field.textContent);
        field.setAttribute('name', result.name);
        field.textContent = result.content;
    },
    renameElement(block, tag, name, newName) {
        const el = block.querySelector(`${tag}[name="${name}"]`);
        if (!el) {
            return;
        }
        el.setAttribute('name', newName);
    },
    renameValue(block, name, newName) {
        return this.renameElement(block, 'value', name, newName);
    },
    renameStatement(block, name, newName) {
        return this.renameElement(block, 'statement', name, newName);
    },
    transformEventBlock(root, event, type, statement) {
        this.transformBlock(root, 'block[type="part_event"]', (block) => {
            const field = block.querySelector('field[name="EVENT"]');
            if (!field) {
                return;
            }
            // Depending on the contents of the field, change the block type
            if (field.textContent === event) {
                block.setAttribute('type', type);
                block.removeChild(field);
                LegacyUtil.renameStatement(block, 'DO', statement);
            }
        });
    },
    forEachPart(app, type, callback) {
        if (!app.parts) {
            return;
        }
        const part = app.parts.filter((part) => part.type === type);
        part.forEach(callback);
    },
};
//# sourceMappingURL=util.js.map