"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const style_parser_1 = require("../style.parser");
const background_image_parser_1 = require("./background-image.parser");
const background_position_parser_1 = require("./background-position.parser");
const background_size_parser_1 = require("./background-size.parser");
const backgroundParsers = new Map([
    ['backgroundcolor', new style_parser_1.StyleParser('backgroundColor')],
    ['backgroundimage', new background_image_parser_1.BackgroundImageParser()],
    ['backgroundsize', new background_size_parser_1.BackgroundSizeParser()],
    ['backgroundresizemode', new style_parser_1.StyleParser('resizeMode')],
    ['backgroundposition', new background_position_parser_1.BackgroundPositionParser()],
]);
class BackgroundParser {
    parseAttr(e, attrName, result) {
        const v = e.getAttribute(attrName);
        const parser = backgroundParsers.get(attrName);
        if (v && parser) {
            result = (0, lodash_1.merge)(result, parser.parse(v));
            e.removeAttribute(attrName);
        }
        return result;
    }
    parse(e) {
        let result = this.parseAttr(e, 'backgroundcolor', {});
        result = this.parseAttr(e, 'backgroundimage', result);
        result = this.parseAttr(e, 'backgroundsize', result);
        const positionValue = e.getAttribute('backgroundposition');
        if (positionValue) {
            const parser = backgroundParsers.get('backgroundposition');
            const imageStyle = result.imageStyle || { width: 0, height: 0 };
            const position = parser.parseWithDimensions(positionValue, imageStyle.width, imageStyle.height);
            e.removeAttribute('backgroundposition');
            result = (0, lodash_1.merge)(result, position);
        }
        else {
            result = this.parseAttr(e, 'backgroundresizemode', result);
        }
        return result;
    }
}
exports.default = new BackgroundParser();
//# sourceMappingURL=background.parser.js.map