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 delete a single securityRequirement from an operation.
 * @author eric.wittmann@gmail.com
 * @extends AbstractCommand
 * @class
 */
var DeleteSecurityRequirementCommand = /** @class */ (function (_super) {
    __extends(DeleteSecurityRequirementCommand, _super);
    function DeleteSecurityRequirementCommand(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._oldRequirement === undefined)
                _this._oldRequirement = null;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._requirement === undefined)
                _this._requirement = null;
            if (_this._oldRequirement === undefined)
                _this._oldRequirement = null;
            (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._oldRequirement === undefined)
                _this._oldRequirement = null;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._requirement === undefined)
                _this._requirement = null;
            if (_this._oldRequirement === undefined)
                _this._oldRequirement = null;
        }
        else
            throw new Error('invalid overload');
        return _this;
    }
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#execute(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    DeleteSecurityRequirementCommand.prototype.execute = function (document) {
        LoggerCompat.info("[DeleteSecurityRequirementCommand] Executing.");
        this._oldRequirement = null;
        var parent = this._parentPath.resolve(document);
        if (this.isNullOrUndefined(parent)) {
            return;
        }
        var requirement = parent.createSecurityRequirement();
        Library.readNode(this._requirement, requirement);
        var requirements = parent.getSecurityRequirements();
        var idx = this.indexOfRequirement(requirements, requirement);
        if (idx !== -1) {
            this._oldRequirement = Library.writeNode(/* get */ requirements[idx]);
            /* remove */ requirements.splice(idx, 1)[0];
        }
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    DeleteSecurityRequirementCommand.prototype.undo = function (document) {
        LoggerCompat.info("[DeleteSecurityRequirementCommand] Reverting.");
        if (this.isNullOrUndefined(this._oldRequirement)) {
            return;
        }
        var parent = this._parentPath.resolve(document);
        if (this.isNullOrUndefined(parent)) {
            return;
        }
        var restoredRequirement = parent.createSecurityRequirement();
        Library.readNode(this._oldRequirement, restoredRequirement);
        parent.addSecurityRequirement(restoredRequirement);
    };
    DeleteSecurityRequirementCommand.prototype.indexOfRequirement = function (requirements, requirement) {
        var idx = 0;
        for (var index197 = 0; index197 < requirements.length; index197++) {
            var r = requirements[index197];
            {
                if (this.isEqual(r, requirement)) {
                    return idx;
                }
                idx++;
            }
        }
        return -1;
    };
    DeleteSecurityRequirementCommand.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 index198 = 0; index198 < names1.length; index198++) {
            var name1 = names1[index198];
            {
                if (names2.indexOf(name1) === -1) {
                    rval = false;
                }
            }
        }
        return rval;
    };
    return DeleteSecurityRequirementCommand;
}(AbstractCommand));
export { DeleteSecurityRequirementCommand };
DeleteSecurityRequirementCommand["__class"] = "io.apicurio.datamodels.cmd.commands.DeleteSecurityRequirementCommand";
DeleteSecurityRequirementCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
//# sourceMappingURL=DeleteSecurityRequirementCommand.js.map