"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var sp_core_library_1 = require("@microsoft/sp-core-library");
var sp_diagnostics_1 = require("@microsoft/sp-diagnostics");
var sp_loader_1 = require("@microsoft/sp-loader");
var sp_page_context_1 = require("@microsoft/sp-page-context");
/**
 * Client-side custom action.
 */
var ClientSideCustomAction = /** @class */ (function () {
    function ClientSideCustomAction() {
    }
    /**
     * Try to create a custom action from the preloaded custom action data. If there is a failure
     * to create the action, log an error and return undefined. We do not want to cause application
     * loading failures in case of custom action creation failures.
     *
     * @internal
     */
    ClientSideCustomAction._tryCreateFromPreloadedCustomAction = function (customAction) {
        var typedCustomAction = undefined;
        var qosMonitor = new sp_diagnostics_1._QosMonitor('ClientSideCustomActions.CreateFromPreloadedCustomAction');
        try {
            typedCustomAction = new ClientSideCustomAction();
            var componentId = customAction.clientSideComponentId;
            sp_core_library_1.Validate.isNonemptyString(componentId, 'customAction.clientSideComponentId');
            sp_core_library_1.Validate.isNonemptyString(customAction.location, 'customAction.location');
            sp_core_library_1.Validate.isNotNullOrUndefined(sp_core_library_1.Guid.tryParse(componentId), 'customAction.clientSideComponentId as Guid');
            if (!sp_loader_1.SPComponentLoader.tryGetManifestById(componentId)) {
                var error = new Error("Custom action with component id \"" + componentId + "\" didn't have a manifest.");
                qosMonitor.writeUnexpectedFailure('ManifestNotFound', error, { customAction: componentId });
                throw error;
            }
            typedCustomAction.title = customAction.title;
            typedCustomAction.location = customAction.location;
            typedCustomAction.clientSideComponentId = sp_core_library_1.Guid.parse(componentId);
            typedCustomAction.clientSideComponentProperties = customAction.clientSideComponentProperties;
            typedCustomAction.registrationType = customAction.registrationType;
            typedCustomAction.registrationId = customAction.registrationId;
            typedCustomAction.sequence = customAction.sequence;
            typedCustomAction.hostProperties = customAction.hostProperties;
            typedCustomAction.requireSiteAdministrator = customAction.requireSiteAdministrator;
            typedCustomAction.id = sp_core_library_1.Guid.tryParse(customAction.id);
            var rights = customAction.rights;
            if (rights && rights.High !== undefined && rights.Low !== undefined) {
                typedCustomAction.rights = new sp_page_context_1.SPPermission(rights);
            }
            else {
                sp_diagnostics_1._TraceLogger.logVerbose(ClientSideCustomAction._logSource, "The rights object has invalid format. One of High or Low fields cannot be undefined. Object value is '" + JSON.stringify(rights));
            }
            qosMonitor.writeSuccess({
                customAction: typedCustomAction.tag
            });
            return typedCustomAction;
        }
        catch (error) {
            sp_diagnostics_1._TraceLogger.logError(ClientSideCustomAction._logSource, new Error("Failed to create client-side custom action. Error: " + error.message));
            qosMonitor.writeUnexpectedFailure('UnhandledCreateError', error, {
                customAction: customAction.clientSideComponentId
            });
            return undefined;
        }
    };
    /**
     * Try create custom action from query string debug data
     */
    ClientSideCustomAction._tryCreateFromDebugData = function (location, clientSideComponentId, clientSideComponentProperties, sequence, hostProperties) {
        try {
            sp_core_library_1.Validate.isNotNullOrUndefined(clientSideComponentId, 'clientSideComponentId');
            sp_core_library_1.Validate.isNonemptyString(location, 'location');
            var customAction = new ClientSideCustomAction();
            customAction.location = location;
            customAction.clientSideComponentId = clientSideComponentId;
            customAction.clientSideComponentProperties = clientSideComponentProperties;
            customAction.sequence = sequence;
            customAction.hostProperties = hostProperties;
            return customAction;
        }
        catch (error) {
            sp_diagnostics_1._TraceLogger.logError(ClientSideCustomAction._logSource, new Error("Failed to create client-side custom action. Error: " + error.message));
            return undefined;
        }
    };
    Object.defineProperty(ClientSideCustomAction.prototype, "tag", {
        /**
         * Returns a tag that should be used for logging and telemetry purposes.
         */
        get: function () {
            return this.location + "." + this.clientSideComponentId.toString();
        },
        enumerable: false,
        configurable: true
    });
    ClientSideCustomAction._logSource = sp_diagnostics_1._LogSource.create('ClientSideCustomAction');
    return ClientSideCustomAction;
}());
exports.default = ClientSideCustomAction;
//# sourceMappingURL=ClientSideCustomAction.js.map