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 { ModelUtils } from '../util/ModelUtils';
import { LoggerCompat } from '../../compat/LoggerCompat';
import { OasOperation } from '../../openapi/models/OasOperation';
import { OasResponse } from '../../openapi/models/OasResponse';
/**
 * A command used to create a new response in an operation.
 * @author eric.wittmann@gmail.com
 * @extends AbstractCommand
 * @class
 */
var NewResponseCommand = /** @class */ (function (_super) {
    __extends(NewResponseCommand, _super);
    function NewResponseCommand(operation, statusCode, sourceResponse) {
        var _this = this;
        if (((operation != null && operation instanceof OasOperation) || operation === null) && ((typeof statusCode === 'string') || statusCode === null) && ((sourceResponse != null && sourceResponse instanceof OasResponse) || sourceResponse === null)) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._operationPath === undefined)
                _this._operationPath = null;
            if (_this._statusCode === undefined)
                _this._statusCode = null;
            if (_this._created === undefined)
                _this._created = false;
            if (_this._nullResponses === undefined)
                _this._nullResponses = false;
            if (_this.sourceResponse === undefined)
                _this.sourceResponse = null;
            if (_this._operationPath === undefined)
                _this._operationPath = null;
            if (_this._statusCode === undefined)
                _this._statusCode = null;
            if (_this._created === undefined)
                _this._created = false;
            if (_this._nullResponses === undefined)
                _this._nullResponses = false;
            if (_this.sourceResponse === undefined)
                _this.sourceResponse = null;
            (function () {
                _this._operationPath = Library.createNodePath(operation);
                _this._statusCode = statusCode;
                if (ModelUtils.isDefined(sourceResponse)) {
                    _this.sourceResponse = Library.writeNode(sourceResponse);
                }
            })();
        }
        else if (operation === undefined && statusCode === undefined && sourceResponse === undefined) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._operationPath === undefined)
                _this._operationPath = null;
            if (_this._statusCode === undefined)
                _this._statusCode = null;
            if (_this._created === undefined)
                _this._created = false;
            if (_this._nullResponses === undefined)
                _this._nullResponses = false;
            if (_this.sourceResponse === undefined)
                _this.sourceResponse = null;
            if (_this._operationPath === undefined)
                _this._operationPath = null;
            if (_this._statusCode === undefined)
                _this._statusCode = null;
            if (_this._created === undefined)
                _this._created = false;
            if (_this._nullResponses === undefined)
                _this._nullResponses = false;
            if (_this.sourceResponse === undefined)
                _this.sourceResponse = 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
     */
    NewResponseCommand.prototype.execute = function (document) {
        LoggerCompat.info("[NewResponseCommand] Executing.  Status Code=%s", this._statusCode);
        this._created = false;
        this._nullResponses = false;
        var operation = this._operationPath.resolve(document);
        if (this.isNullOrUndefined(operation)) {
            return;
        }
        if (this.isNullOrUndefined(operation.responses)) {
            operation.responses = operation.createResponses();
            this._nullResponses = true;
        }
        var response = operation.responses.getResponse(this._statusCode);
        if (this.isNullOrUndefined(response)) {
            response = operation.responses.createResponse(this._statusCode);
            if (ModelUtils.isDefined(this.sourceResponse)) {
                Library.readNode(this.sourceResponse, response);
            }
            operation.responses.addResponse(this._statusCode, response);
            this._created = true;
        }
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    NewResponseCommand.prototype.undo = function (document) {
        LoggerCompat.info("[NewResponseCommand] Reverting.");
        var operation = this._operationPath.resolve(document);
        if (this.isNullOrUndefined(operation)) {
            return;
        }
        if (this._nullResponses) {
            operation.responses = null;
            return;
        }
        if (!this._created) {
            return;
        }
        operation.responses.removeResponse(this._statusCode);
    };
    return NewResponseCommand;
}(AbstractCommand));
export { NewResponseCommand };
NewResponseCommand["__class"] = "io.apicurio.datamodels.cmd.commands.NewResponseCommand";
NewResponseCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
//# sourceMappingURL=NewResponseCommand.js.map