var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
/* Generated from Java with JSweet 2.3.7 - http://www.jsweet.org */
import { Library } from '../../Library';
import { AbstractCommand } from '../AbstractCommand';
import { JsonCompat } from '../../compat/JsonCompat';
import { LoggerCompat } from '../../compat/LoggerCompat';
import { DocumentType } from '../../core/models/DocumentType';
/**
 * A command used to delete all security schemes from a document or operation.
 * @author eric.wittmann@gmail.com
 * @extends AbstractCommand
 * @class
 */
var DeleteAllSecuritySchemesCommand = /** @class */ (function (_super) {
    __extends(DeleteAllSecuritySchemesCommand, _super);
    function DeleteAllSecuritySchemesCommand() {
        var _this = _super.call(this) || this;
        if (_this._oldSecuritySchemes === undefined)
            _this._oldSecuritySchemes = null;
        return _this;
    }
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#execute(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    DeleteAllSecuritySchemesCommand.prototype.execute = function (document) {
        var _this = this;
        LoggerCompat.info("[DeleteAllSecuritySchemesCommand] Executing.");
        this._oldSecuritySchemes = ([]);
        if (document.getDocumentType() === DocumentType.openapi2) {
            var doc = document;
            if (!this.isNullOrUndefined(doc.securityDefinitions)) {
                doc.securityDefinitions.getSecuritySchemes().forEach(function (scheme) {
                    var savedScheme = Library.writeNode(scheme);
                    JsonCompat.setPropertyString(savedScheme, "__name", scheme.getSchemeName());
                    /* add */ (_this._oldSecuritySchemes.push(savedScheme) > 0);
                });
            }
            doc.securityDefinitions = null;
        }
        if (document.getDocumentType() === DocumentType.openapi3) {
            var doc = document;
            if (!this.isNullOrUndefined(doc.components)) {
                doc.components.getSecuritySchemes().forEach((function (doc) {
                    return function (scheme) {
                        var savedScheme = Library.writeNode(scheme);
                        JsonCompat.setPropertyString(savedScheme, "__name", scheme.getSchemeName());
                        /* add */ (_this._oldSecuritySchemes.push(savedScheme) > 0);
                        doc.components.removeSecurityScheme(scheme.getSchemeName());
                    };
                })(doc));
            }
        }
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    DeleteAllSecuritySchemesCommand.prototype.undo = function (document) {
        LoggerCompat.info("[DeleteAllSecuritySchemesCommand] Reverting.");
        if (this.isNullOrUndefined(this._oldSecuritySchemes) || /* size */ this._oldSecuritySchemes.length === 0) {
            return;
        }
        if (document.getDocumentType() === DocumentType.openapi2) {
            var doc = document;
            if (this.isNullOrUndefined(doc.securityDefinitions)) {
                doc.securityDefinitions = doc.createSecurityDefinitions();
            }
            this._oldSecuritySchemes.forEach((function (doc) {
                return function (savedScheme) {
                    var name = JsonCompat.consumePropertyString(savedScheme, "__name");
                    var scheme = doc.securityDefinitions.createSecurityScheme(name);
                    Library.readNode(savedScheme, scheme);
                    doc.securityDefinitions.addSecurityScheme(name, scheme);
                };
            })(doc));
        }
        if (document.getDocumentType() === DocumentType.openapi3) {
            var doc = document;
            if (this.isNullOrUndefined(doc.components)) {
                doc.components = doc.createComponents();
            }
            this._oldSecuritySchemes.forEach((function (doc) {
                return function (savedScheme) {
                    var name = JsonCompat.consumePropertyString(savedScheme, "__name");
                    var scheme = doc.components.createSecurityScheme(name);
                    Library.readNode(savedScheme, scheme);
                    doc.components.addSecurityScheme(name, scheme);
                };
            })(doc));
        }
    };
    return DeleteAllSecuritySchemesCommand;
}(AbstractCommand));
export { DeleteAllSecuritySchemesCommand };
DeleteAllSecuritySchemesCommand["__class"] = "io.apicurio.datamodels.cmd.commands.DeleteAllSecuritySchemesCommand";
DeleteAllSecuritySchemesCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
//# sourceMappingURL=DeleteAllSecuritySchemesCommand.js.map