"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractComponentAttr = extractComponentAttr;
exports.extractAttr = extractAttr;
exports.applyAttr = applyAttr;
exports.removeAllAttr = removeAllAttr;
exports.mergeAttr = mergeAttr;
const isFrameRelative_1 = require("../isFrameRelative");
function extractComponentAttr(component, trait) {
    const attr = extractAttr(component.$element);
    if (component.$element instanceof HTMLCanvasElement) {
        let propWidth = component.getProp('width');
        let propHeight = component.getProp('height');
        if (typeof propWidth === 'number') {
            propWidth = propWidth + 'px';
        }
        if (typeof propHeight === 'number') {
            propHeight = propHeight + 'px';
        }
        if ((0, isFrameRelative_1.isFrameRelativeValue)(propWidth ?? '')) {
            attr['width'] = `${trait.width}px`;
        }
        if ((0, isFrameRelative_1.isFrameRelativeValue)(propHeight ?? '')) {
            attr['height'] = `${trait.height}px`;
        }
    }
    return attr;
}
function extractAttr(element) {
    if (element instanceof Text) {
        return {};
    }
    const attr = {};
    for (let attribute of element.attributes) {
        attr[attribute.name] = attribute.value;
    }
    return attr;
}
function applyAttr(element, attr, current) {
    for (const name in current) {
        const value = attr[name];
        if (value === undefined) {
            element.removeAttribute(name);
        }
    }
    mergeAttr(element, attr);
}
function removeAllAttr(element) {
    const attributes = element.attributes;
    for (const attr of attributes) {
        attributes.removeNamedItem(attr.name);
    }
}
function mergeAttr(element, attr) {
    for (const name in attr) {
        const value = attr[name];
        element.setAttribute(name, value);
    }
}
//# sourceMappingURL=attr.js.map