"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_loader_1 = require("@microsoft/sp-loader");
var ApplicationCustomizerContext_1 = tslib_1.__importDefault(require("./ApplicationCustomizerContext"));
var ApplicationCustomizerContextFactory = /** @class */ (function () {
    function ApplicationCustomizerContextFactory(serviceScope) {
        this.applicationCustomizerTopHeight = 'preAllocatedApplicationCustomizerTopHeight';
        this.applicationCustomizerBottomHeight = 'preAllocatedApplicationCustomizerBottomHeight';
        sp_core_library_1.Validate.isNotNullOrUndefined(serviceScope, 'serviceScope');
        this._serviceScope = serviceScope;
    }
    ApplicationCustomizerContextFactory.prototype.createApplicationCustomizerContext = function (customAction, extensionContextParameters, includeAllParams) {
        if (extensionContextParameters === void 0) { extensionContextParameters = {}; }
        if (includeAllParams === void 0) { includeAllParams = true; }
        var componentId = customAction.clientSideComponentId.toString();
        var topHeight = 0;
        var bottomHeight = 0;
        if (customAction.hostProperties) {
            topHeight =
                parseInt(JSON.parse(customAction.hostProperties)[this.applicationCustomizerTopHeight], 10) || 0;
            bottomHeight =
                parseInt(JSON.parse(customAction.hostProperties)[this.applicationCustomizerBottomHeight], 10) || 0;
        }
        var applicationCustomizerContextParameters = {
            sequence: customAction.sequence !== undefined ? customAction.sequence : 65535,
            preAllocatedApplicationCustomizerTopHeight: topHeight,
            preAllocatedApplicationCustomizerBottomHeight: bottomHeight
        };
        var applicationCustomizerContext;
        if (includeAllParams) {
            var manifest = sp_loader_1.SPComponentLoader.tryGetManifestById(componentId);
            if (!manifest) {
                var error = new Error("Custom action with component id \"" + componentId + "\" didn't have a manifest.");
                throw error;
            }
            var instanceId = sp_core_library_1.Guid.newGuid().toString();
            var loggingTag = "Extension." + manifest.id + "." + instanceId;
            applicationCustomizerContext = new ApplicationCustomizerContext_1.default({
                manifest: manifest,
                parentServiceScope: this._serviceScope,
                instanceId: instanceId,
                loggingTag: loggingTag
            }, applicationCustomizerContextParameters);
        }
        else {
            applicationCustomizerContext = new ApplicationCustomizerContext_1.default(extensionContextParameters, applicationCustomizerContextParameters);
        }
        return applicationCustomizerContext;
    };
    return ApplicationCustomizerContextFactory;
}());
exports.default = ApplicationCustomizerContextFactory;
//# sourceMappingURL=ApplicationCustomizerContextFactory.js.map