"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var sp_core_library_1 = require("@microsoft/sp-core-library");
var sp_core_library_2 = require("@microsoft/sp-core-library");
var Placeholder_1 = tslib_1.__importDefault(require("./Placeholder"));
var PlaceholderName_1 = tslib_1.__importDefault(require("./PlaceholderName"));
/**
 * PlaceholderManager is a service registered with the application's root ServiceScope.
 * The host application uses this object to define the placeholders which will
 * be consumed by third-party extensions.  The PlaceholderCollection then obtains its
 * data from the PlaceholderManager.
 *
 * @internal
 */
var PlaceholderManager = /** @class */ (function () {
    function PlaceholderManager(serviceScope) {
        this._isEnabled = false;
        this._changedEvent = new sp_core_library_2.SPEvent(PlaceholderManager.placeholdersChangedEventName);
        this._placeholders = new Map();
        // Create the placeholders for all the names
        this._placeholders.set(PlaceholderName_1.default.Top, this._createPlaceholder(PlaceholderName_1.default.Top));
        this._placeholders.set(PlaceholderName_1.default.Bottom, this._createPlaceholder(PlaceholderName_1.default.Bottom));
    }
    Object.defineProperty(PlaceholderManager.prototype, "changedEvent", {
        /**
         * The event raised when the application changes placeholder definitions
         * @eventproperty
         */
        get: function () {
            return this._changedEvent;
        },
        enumerable: false,
        configurable: true
    });
    /**
     * Initializes the collection of placeholders for use by application customizers.
     */
    PlaceholderManager.prototype.initializePlaceholders = function (definitions) {
        var _this = this;
        sp_core_library_1.Validate.isNotNullOrUndefined(definitions, 'definitions');
        definitions.forEach(function (def) { return _this.addPlaceholder(def.name, def.domElement); });
    };
    /**
     * Adds a placeholder.
     * If a placeholder already existed for the same name, it replaces it.
     */
    PlaceholderManager.prototype.addPlaceholder = function (name, domElement) {
        sp_core_library_1.Validate.isTrue(this.placeholders.has(name), 'placeholder exists');
        this._placeholders.get(name).attachToApplication(domElement);
        sp_core_library_2._SPEventManager.instance.raiseStickyEvent(PlaceholderManager.placeholdersChangedEventName, new sp_core_library_2.SPEventArgs());
    };
    /**
     * Removes a placeholder.
     */
    PlaceholderManager.prototype.removePlaceholder = function (name) {
        sp_core_library_1.Validate.isTrue(this.placeholders.has(name), 'placeholder exists');
        this._placeholders.get(name).detachFromApplication();
        sp_core_library_2._SPEventManager.instance.raiseStickyEvent(PlaceholderManager.placeholdersChangedEventName, new sp_core_library_2.SPEventArgs());
    };
    /**
     * Register the list of valid placeholders for Workbench
     */
    PlaceholderManager.prototype.registerPlaceholders = function () {
        // Select all Nodes with specified attribute name
        var placeholderAttributeName = 'data-sp-placeholder';
        var placeholderDivs = document.querySelectorAll("[" + placeholderAttributeName + "]");
        var regionDef = [];
        // Check div exist and placeholder name is part of PlaceholderName enum
        for (var i = 0; i < placeholderDivs.length; i++) {
            var div = placeholderDivs[i];
            var name_1 = div.getAttribute(placeholderAttributeName);
            if (div && name_1) {
                var placeholderName = PlaceholderName_1.default[name_1];
                if (typeof placeholderName !== 'number') {
                    throw new Error('Invalid placeholder definition: ' + name_1);
                }
                regionDef.push({
                    name: placeholderName,
                    domElement: div
                });
            }
        }
        this.initializePlaceholders(regionDef);
    };
    Object.defineProperty(PlaceholderManager.prototype, "isEnabled", {
        /**
         * Returns true if the placeholders are enabled for the current application
         */
        get: function () {
            return this._isEnabled;
        },
        enumerable: false,
        configurable: true
    });
    /**
     * Allows the application to set if placeholders are enabled for the current application
     * @internal
     */
    PlaceholderManager.prototype._enable = function () {
        this._isEnabled = true;
    };
    Object.defineProperty(PlaceholderManager.prototype, "placeholders", {
        /**
         * Returns a map from PlaceholderName to Placeholder for all active placeholders.
         */
        get: function () {
            return this._placeholders;
        },
        enumerable: false,
        configurable: true
    });
    PlaceholderManager.prototype._createPlaceholder = function (name) {
        return new Placeholder_1.default({ name: name, domElement: document.createElement('div') }, this);
    };
    /**
     * The service key for PlaceholderManager.
     */
    PlaceholderManager.serviceKey = sp_core_library_1.ServiceKey.create('sp-application-base:PlaceholderManager', PlaceholderManager);
    /**
     * SPEvent name for placeholders change
     */
    PlaceholderManager.placeholdersChangedEventName = 'placeholders.changedEvent';
    return PlaceholderManager;
}());
exports.default = PlaceholderManager;
//# sourceMappingURL=PlaceholderManager.js.map