/**
 * Reading facilities common to all readers that read specific versions.
 * @author Louis-Dominique Dubeau
 * @license MPL 2.0
 * @copyright Mangalam Research Center for Buddhist Languages
 */
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
define(["require", "exports", "./metadata-json-reader", "./metadata-schema.json"], function (require, exports, metadata_json_reader_1, metadataSchema) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    metadataSchema = __importStar(metadataSchema);
    // tslint:disable-next-line:completed-docs
    class MetadataBase {
        constructor(expectedVersion, metadata) {
            this.metadata = metadata;
            this.reverseMappings = Object.create(null);
            this.descMap = Object.create(null);
            if (metadata.version !== expectedVersion) {
                throw new Error(`incorrect version number: expected ${expectedVersion}, \
got ${metadata.version}`);
            }
            this.version = metadata.version;
            this.generator = metadata.generator;
            this.date = metadata.date;
            if (metadata.namespaces !== undefined) {
                this.namespaceMappings = metadata.namespaces;
            }
            else {
                this.namespaceMappings = Object.create(null);
            }
            if ("xml" in this.namespaceMappings) {
                throw new Error("xml mapping already defined");
            }
            // tslint:disable-next-line:no-http-string
            this.namespaceMappings.xml = "http://www.w3.org/XML/1998/namespace";
            // tslint:disable-next-line:forin
            for (const prefix in this.namespaceMappings) {
                const ns = this.namespaceMappings[prefix];
                // If prefix foo resolves to http://bar and bar resolves to the same URI
                // and foo is before bar, then foo wins.
                if (this.reverseMappings[ns] === undefined) {
                    this.reverseMappings[ns] = prefix;
                }
            }
            this.reverseMappings[this.namespaceMappings[""]] = "";
            const elements = metadata.elements;
            if (elements !== undefined) {
                const descMap = this.descMap;
                for (const el of elements) {
                    // Here, an undefined namespace is the tei namespace.
                    const elNs = el.ns !== undefined ? el.ns :
                        // tslint:disable-next-line:no-http-string
                        "http://www.tei-c.org/ns/1.0";
                    const elPrefix = this.reverseMappings[elNs];
                    if (elPrefix === undefined) {
                        throw new Error(`undefined namespace: ${elNs}`);
                    }
                    const name = elPrefix === "" ? el.name : `${elPrefix}:${el.name}`;
                    descMap[name] = el.desc;
                }
            }
        }
        getNamespaceMappings() {
            return this.namespaceMappings;
        }
        shortDescriptionFor(name) {
            const unresolved = this.unresolveName(name);
            if (unresolved === undefined) {
                return undefined;
            }
            return this.descMap[unresolved];
        }
        unresolveName(name) {
            const prefix = this.reverseMappings[name.ns];
            if (prefix === undefined) {
                return undefined;
            }
            return (prefix === "") ? name.name : `${prefix}:${name.name}`;
        }
    }
    exports.MetadataBase = MetadataBase;
    /**
     * A reader that reads a versioned format of the metadata.
     */
    class MetadataReaderBase extends metadata_json_reader_1.MetadataJSONReader {
        constructor(metadataClass) {
            super(metadataSchema);
            this.metadataClass = metadataClass;
        }
        convert(object) {
            return new this.metadataClass(object);
        }
    }
    MetadataReaderBase.version = "1";
    exports.MetadataReaderBase = MetadataReaderBase;
});
//  LocalWords:  MPL expectedVersion xml tei elNs elPrefix el
//# sourceMappingURL=metadata-versioned-reader.js.map