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 { LoggerCompat } from '../../compat/LoggerCompat';
import { SecurityRequirement } from '../../core/models/common/SecurityRequirement';
/**
 * A command used to create a new security requirement in a document.
 * @author eric.wittmann@gmail.com
 * @extends AbstractCommand
 * @class
 */
var AddSecurityRequirementCommand = /** @class */ (function (_super) {
    __extends(AddSecurityRequirementCommand, _super);
    function AddSecurityRequirementCommand(parent, requirement) {
        var _this = this;
        if (((parent != null && (parent["__interfaces"] != null && parent["__interfaces"].indexOf("io.apicurio.datamodels.core.models.common.ISecurityRequirementParent") >= 0 || parent.constructor != null && parent.constructor["__interfaces"] != null && parent.constructor["__interfaces"].indexOf("io.apicurio.datamodels.core.models.common.ISecurityRequirementParent") >= 0)) || parent === null) && ((requirement != null && requirement instanceof SecurityRequirement) || requirement === null)) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._requirement === undefined)
                _this._requirement = null;
            if (_this._added === undefined)
                _this._added = false;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._requirement === undefined)
                _this._requirement = null;
            if (_this._added === undefined)
                _this._added = false;
            (function () {
                _this._parentPath = Library.createNodePath(parent);
                _this._requirement = Library.writeNode(requirement);
            })();
        }
        else if (parent === undefined && requirement === undefined) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._requirement === undefined)
                _this._requirement = null;
            if (_this._added === undefined)
                _this._added = false;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._requirement === undefined)
                _this._requirement = null;
            if (_this._added === undefined)
                _this._added = false;
        }
        else
            throw new Error('invalid overload');
        return _this;
    }
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#execute(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    AddSecurityRequirementCommand.prototype.execute = function (document) {
        LoggerCompat.info("[AddSecurityRequirementCommand] Executing.");
        this._added = false;
        var parent = this._parentPath.resolve(document);
        if (this.isNullOrUndefined(parent)) {
            return;
        }
        var requirement = parent.createSecurityRequirement();
        Library.readNode(this._requirement, requirement);
        parent.addSecurityRequirement(requirement);
        this._added = true;
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    AddSecurityRequirementCommand.prototype.undo = function (document) {
        LoggerCompat.info("[AddSecurityRequirementCommand] Reverting.");
        if (!this._added) {
            return;
        }
        var parent = this._parentPath.resolve(document);
        if (this.isNullOrUndefined(parent)) {
            return;
        }
        var security = parent.getSecurityRequirements();
        if (this.isNullOrUndefined(security)) {
            LoggerCompat.info("[AddSecurityRequirementCommand] Security requirement not found, skipping undo.");
            return;
        }
        var requirement = parent.createSecurityRequirement();
        Library.readNode(this._requirement, requirement);
        var idx = this.indexOfRequirement(security, requirement);
        if (idx !== -1) {
            /* remove */ security.splice(idx, 1)[0];
        }
    };
    AddSecurityRequirementCommand.prototype.indexOfRequirement = function (requirements, requirement) {
        var idx = 0;
        for (var index213 = 0; index213 < requirements.length; index213++) {
            var r = requirements[index213];
            {
                if (this.isEqual(r, requirement)) {
                    return idx;
                }
                idx++;
            }
        }
        return -1;
    };
    AddSecurityRequirementCommand.prototype.isEqual = function (req1, req2) {
        var names1 = req1.getSecurityRequirementNames();
        var names2 = req2.getSecurityRequirementNames();
        if ( /* size */names1.length !== /* size */ names2.length) {
            return false;
        }
        var rval = true;
        for (var index214 = 0; index214 < names1.length; index214++) {
            var name1 = names1[index214];
            {
                if (names2.indexOf(name1) === -1) {
                    rval = false;
                }
            }
        }
        return rval;
    };
    return AddSecurityRequirementCommand;
}(AbstractCommand));
export { AddSecurityRequirementCommand };
AddSecurityRequirementCommand["__class"] = "io.apicurio.datamodels.cmd.commands.AddSecurityRequirementCommand";
AddSecurityRequirementCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
//# sourceMappingURL=AddSecurityRequirementCommand.js.map