/**
 * Load and initialize modes.
 * @author Louis-Dominique Dubeau
 * @license MPL 2.0
 * @copyright Mangalam Research Center for Buddhist Languages
 */
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    /**
     * A class that can load modes.
     */
    class ModeLoader {
        /**
         * @param runtime The runtime to use to load the mode module.
         */
        constructor(editor, runtime) {
            this.editor = editor;
            this.runtime = runtime;
        }
        /**
         * Load and initialize a mode.
         *
         * @param path The path to the mode.
         *
         * @param options The mode's options.
         *
         * @returns A promise that resolves to the initialized [[Mode]] object.
         */
        initMode(path, options = {}) {
            return __awaiter(this, void 0, void 0, function* () {
                const mmodule = yield this.loadMode(path);
                const mode = new mmodule.Mode(this.editor, options);
                yield mode.init();
                return mode;
            });
        }
        /**
         * Loads a mode.
         *
         * @param path The path to the mode.
         *
         * @returns A promise that resolves to the module that holds the mode.
         */
        loadMode(path) {
            return __awaiter(this, void 0, void 0, function* () {
                const runtime = this.runtime;
                try {
                    return (yield runtime.resolveModules(path))[0];
                }
                // tslint:disable-next-line:no-empty
                catch (ex) { }
                if (path.indexOf("/") !== -1) {
                    // It is an actual path so don't try any further loading.
                    throw new Error(`can't load mode ${path}`);
                }
                path = `wed/modes/${path}/${path}`;
                try {
                    return (yield runtime.resolveModules(path))[0];
                }
                // tslint:disable-next-line:no-empty
                catch (ex) { }
                try {
                    return (yield runtime.resolveModules(`${path}-mode`))[0];
                }
                // tslint:disable-next-line:no-empty
                catch (ex) { }
                return (yield runtime.resolveModules(`${path}_mode`))[0];
            });
        }
    }
    exports.ModeLoader = ModeLoader;
});
//  LocalWords:  MPL runtime
//# sourceMappingURL=mode-loader.js.map