"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var tagsTypings = {
    a: {
        selfclosing: false,
        attrsRequired: ['href', 'title'],
        redundantAttrs: ['alt'],
    },
    div: {
        selfclosing: false,
    },
    main: {
        selfclosing: false,
        redundantAttrs: ['role'],
    },
    nav: {
        selfclosing: false,
        redundantAttrs: ['role'],
    },
    script: {
        attrsOptional: [
            ['async', 'async'],
            ['defer', 'defer'],
        ],
    },
    img: {
        selfclosing: true,
        attrsRequired: ['src', 'alt', 'title'],
    },
};
exports.default = {
    id: 'tags-check',
    description: 'Checks html tags.',
    init: function (parser, reporter, options) {
        var _this = this;
        tagsTypings = __assign(__assign({}, tagsTypings), options);
        parser.addListener('tagstart', function (event) {
            var attrs = event.attrs;
            var col = event.col + event.tagName.length + 1;
            var tagName = event.tagName.toLowerCase();
            if (tagsTypings[tagName]) {
                var currentTagType = tagsTypings[tagName];
                if (currentTagType.selfclosing === true && !event.close) {
                    reporter.warn("The <" + tagName + "> tag must be selfclosing.", event.line, event.col, _this, event.raw);
                }
                else if (currentTagType.selfclosing === false && event.close) {
                    reporter.warn("The <" + tagName + "> tag must not be selfclosing.", event.line, event.col, _this, event.raw);
                }
                if (Array.isArray(currentTagType.attrsRequired)) {
                    var attrsRequired = currentTagType.attrsRequired;
                    attrsRequired.forEach(function (id) {
                        if (Array.isArray(id)) {
                            var copyOfId = id.map(function (a) { return a; });
                            var realID_1 = copyOfId.shift();
                            var values_1 = copyOfId;
                            if (attrs.some(function (attr) { return attr.name === realID_1; })) {
                                attrs.forEach(function (attr) {
                                    if (attr.name === realID_1 &&
                                        values_1.indexOf(attr.value) === -1) {
                                        reporter.error("The <" + tagName + "> tag must have attr '" + realID_1 + "' with one value of '" + values_1.join("' or '") + "'.", event.line, col, _this, event.raw);
                                    }
                                });
                            }
                            else {
                                reporter.error("The <" + tagName + "> tag must have attr '" + realID_1 + "'.", event.line, col, _this, event.raw);
                            }
                        }
                        else if (!attrs.some(function (attr) { return id.split('|').indexOf(attr.name) !== -1; })) {
                            reporter.error("The <" + tagName + "> tag must have attr '" + id + "'.", event.line, col, _this, event.raw);
                        }
                    });
                }
                if (Array.isArray(currentTagType.attrsOptional)) {
                    var attrsOptional = currentTagType.attrsOptional;
                    attrsOptional.forEach(function (id) {
                        if (Array.isArray(id)) {
                            var copyOfId = id.map(function (a) { return a; });
                            var realID_2 = copyOfId.shift();
                            var values_2 = copyOfId;
                            if (attrs.some(function (attr) { return attr.name === realID_2; })) {
                                attrs.forEach(function (attr) {
                                    if (attr.name === realID_2 &&
                                        values_2.indexOf(attr.value) === -1) {
                                        reporter.error("The <" + tagName + "> tag must have optional attr '" + realID_2 + "' with one value of '" + values_2.join("' or '") + "'.", event.line, col, _this, event.raw);
                                    }
                                });
                            }
                        }
                    });
                }
                if (Array.isArray(currentTagType.redundantAttrs)) {
                    var redundantAttrs = currentTagType.redundantAttrs;
                    redundantAttrs.forEach(function (attrName) {
                        if (attrs.some(function (attr) { return attr.name === attrName; })) {
                            reporter.error("The attr '" + attrName + "' is redundant for <" + tagName + "> and should be ommited.", event.line, col, _this, event.raw);
                        }
                    });
                }
            }
        });
    },
};
//# sourceMappingURL=data:application/json;base64, 