/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Red Hat, Inc. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "./yamlParser07"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const yamlParser07_1 = require("./yamlParser07");
    class YamlDocuments {
        constructor() {
            // a mapping of URIs to cached documents
            this.cache = new Map();
        }
        /**
         * Get cached YAMLDocument
         * @param document TextDocument to parse
         * @param customTags YAML custom tags
         * @param addRootObject if true and document is empty add empty object {} to force schema usage
         * @returns the YAMLDocument
         */
        getYamlDocument(document, customTags = [], addRootObject = false) {
            this.ensureCache(document, customTags, addRootObject);
            return this.cache.get(document.uri).document;
        }
        /**
         * For test purpose only!
         */
        clear() {
            this.cache.clear();
        }
        ensureCache(document, customTags, addRootObject) {
            const key = document.uri;
            if (!this.cache.has(key)) {
                this.cache.set(key, { version: -1, document: new yamlParser07_1.YAMLDocument([]) });
            }
            if (this.cache.get(key).version !== document.version) {
                let text = document.getText();
                // if text is contains only whitespace wrap all text in object to force schema selection
                if (addRootObject && !/\S/.test(text)) {
                    text = `{${text}}`;
                }
                const doc = yamlParser07_1.parse(text, customTags);
                this.cache.get(key).document = doc;
                this.cache.get(key).version = document.version;
            }
        }
    }
    exports.YamlDocuments = YamlDocuments;
    exports.yamlDocumentsCache = new YamlDocuments();
});
//# sourceMappingURL=yaml-documents.js.map