/**
 * This module is responsible for validating the document being edited in wed.
 * @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", "salve-dom", "./dloc", "./domtypeguards"], function (require, exports, salve_dom_1, dloc, domtypeguards_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    dloc = __importStar(dloc);
    exports.INCOMPLETE = salve_dom_1.WorkingState.INCOMPLETE;
    exports.WORKING = salve_dom_1.WorkingState.WORKING;
    exports.INVALID = salve_dom_1.WorkingState.INVALID;
    exports.VALID = salve_dom_1.WorkingState.VALID;
    /**
     * A document validator.
     */
    class Validator extends salve_dom_1.Validator {
        /**
         * @param schema The schema to use for validating.
         *
         * @param root The root of the DOM tree to validate. This root contains the
         * document to validate but is not **part** of it.
         *
         * @param modeValidators The mode-specific validators to use.
         */
        constructor(schema, root, modeValidators) {
            super(schema, root, {
                timeout: 0,
                maxTimespan: 100,
            });
            this.modeValidators = modeValidators;
        }
        /**
         * Runs document-wide validation specific to the mode passed to
         * the validator.
         */
        _runDocumentValidation() {
            for (const validator of this.modeValidators) {
                const errors = validator.validateDocument();
                for (const error of errors) {
                    this._processError(error);
                }
            }
        }
        possibleAt(container, index = false, attributes = false) {
            if (container instanceof dloc.DLoc) {
                if (typeof index !== "boolean") {
                    throw new Error("2nd parameter must be boolean");
                }
                attributes = index;
                index = container.offset;
                container = container.node;
            }
            if (typeof index !== "number") {
                throw new Error("index must be a number");
            }
            return super.possibleAt(container, index, attributes);
        }
        speculativelyValidate(container, index, toParse) {
            if (container instanceof dloc.DLoc) {
                if (!(domtypeguards_1.isNode(index) || index instanceof Array)) {
                    throw new Error("2nd argument must be a Node or an array of Nodes");
                }
                toParse = index;
                index = container.offset;
                container = container.node;
            }
            if (typeof index !== "number") {
                throw new Error("index must be a number");
            }
            if (toParse === undefined) {
                throw new Error("toParse must be defined");
            }
            return super.speculativelyValidate(container, index, toParse);
        }
        speculativelyValidateFragment(container, index, toParse) {
            if (container instanceof dloc.DLoc) {
                if ((typeof index === "number") || !domtypeguards_1.isElement(index)) {
                    // It appears as "toParse" to the caller, not "index".
                    throw new Error("toParse is not an element");
                }
                toParse = index;
                index = container.offset;
                container = container.node;
            }
            if (typeof index !== "number") {
                throw new Error("index must be a number");
            }
            if (toParse === undefined) {
                throw new Error("toParse must be defined");
            }
            return super.speculativelyValidateFragment(container, index, toParse);
        }
    }
    exports.Validator = Validator;
});
//  LocalWords:  boolean Dubeau Mangalam validator MPL RequireJS unclosed DOM
//  LocalWords:  speculativelyValidate nd toParse
//# sourceMappingURL=validator.js.map