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 replace a definition schema with a newer version.
 * @author eric.wittmann@gmail.com
 * @extends AbstractCommand
 * @class
 */
var ReplaceSecurityRequirementCommand = /** @class */ (function (_super) {
    __extends(ReplaceSecurityRequirementCommand, _super);
    function ReplaceSecurityRequirementCommand(old, replacement) {
        var _this = this;
        if (((old != null && old instanceof SecurityRequirement) || old === null) && ((replacement != null && replacement instanceof SecurityRequirement) || replacement === null)) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._oldRequirement === undefined)
                _this._oldRequirement = null;
            if (_this._newRequirement === undefined)
                _this._newRequirement = null;
            if (_this._replaced === undefined)
                _this._replaced = false;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._oldRequirement === undefined)
                _this._oldRequirement = null;
            if (_this._newRequirement === undefined)
                _this._newRequirement = null;
            if (_this._replaced === undefined)
                _this._replaced = false;
            (function () {
                _this._parentPath = Library.createNodePath(old.parent());
                _this._oldRequirement = Library.writeNode(old);
                _this._newRequirement = Library.writeNode(replacement);
            })();
        }
        else if (old === undefined && replacement === undefined) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._oldRequirement === undefined)
                _this._oldRequirement = null;
            if (_this._newRequirement === undefined)
                _this._newRequirement = null;
            if (_this._replaced === undefined)
                _this._replaced = false;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._oldRequirement === undefined)
                _this._oldRequirement = null;
            if (_this._newRequirement === undefined)
                _this._newRequirement = null;
            if (_this._replaced === undefined)
                _this._replaced = 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
     */
    ReplaceSecurityRequirementCommand.prototype.execute = function (document) {
        LoggerCompat.info("[ReplaceSecurityRequirementCommand] Executing.");
        this._replaced = false;
        var parent = this._parentPath.resolve(document);
        if (this.isNullOrUndefined(parent)) {
            return;
        }
        var oldRequirement = parent.createSecurityRequirement();
        Library.readNode(this._oldRequirement, oldRequirement);
        var oldIdx = this.indexOfRequirement(parent.getSecurityRequirements(), oldRequirement);
        if (oldIdx === -1) {
            return;
        }
        var newRequirement = parent.createSecurityRequirement();
        Library.readNode(this._newRequirement, newRequirement);
        /* set */ (parent.getSecurityRequirements()[oldIdx] = newRequirement);
        this._replaced = true;
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    ReplaceSecurityRequirementCommand.prototype.undo = function (document) {
        LoggerCompat.info("[ReplaceSecurityRequirementCommand] Reverting.");
        if (!this._replaced) {
            return;
        }
        var parent = this._parentPath.resolve(document);
        if (this.isNullOrUndefined(parent)) {
            return;
        }
        var replacementRequirement = parent.createSecurityRequirement();
        Library.readNode(this._newRequirement, replacementRequirement);
        var idx = this.indexOfRequirement(parent.getSecurityRequirements(), replacementRequirement);
        if (idx === -1) {
            return;
        }
        var originalRequirement = parent.createSecurityRequirement();
        Library.readNode(this._oldRequirement, originalRequirement);
        /* set */ (parent.getSecurityRequirements()[idx] = originalRequirement);
    };
    ReplaceSecurityRequirementCommand.prototype.indexOfRequirement = function (requirements, requirement) {
        var idx = 0;
        for (var index215 = 0; index215 < requirements.length; index215++) {
            var r = requirements[index215];
            {
                if (this.isEqual(r, requirement)) {
                    return idx;
                }
                idx++;
            }
        }
        return -1;
    };
    ReplaceSecurityRequirementCommand.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 index216 = 0; index216 < names1.length; index216++) {
            var name1 = names1[index216];
            {
                if (names2.indexOf(name1) === -1) {
                    rval = false;
                }
            }
        }
        return rval;
    };
    return ReplaceSecurityRequirementCommand;
}(AbstractCommand));
export { ReplaceSecurityRequirementCommand };
ReplaceSecurityRequirementCommand["__class"] = "io.apicurio.datamodels.cmd.commands.ReplaceSecurityRequirementCommand";
ReplaceSecurityRequirementCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
//# sourceMappingURL=ReplaceSecurityRequirementCommand.js.map