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 { SimplifiedParameterType } from '../models/SimplifiedParameterType';
import { ModelUtils } from '../util/ModelUtils';
import { SimplifiedTypeUtil } from '../util/SimplifiedTypeUtil';
import { LoggerCompat } from '../../compat/LoggerCompat';
import { NodeCompat } from '../../compat/NodeCompat';
import { Constants } from '../../core/Constants';
import { DocumentType } from '../../core/models/DocumentType';
/**
 * A command used to create a new parameter.
 * @author eric.wittmann@gmail.com
 * @extends AbstractCommand
 * @class
 */
var NewParamCommand = /** @class */ (function (_super) {
    __extends(NewParamCommand, _super);
    function NewParamCommand(parent, paramName, paramType, description, newType, override) {
        var _this = this;
        if (((parent != null && (parent["__interfaces"] != null && parent["__interfaces"].indexOf("io.apicurio.datamodels.openapi.models.IOasParameterParent") >= 0 || parent.constructor != null && parent.constructor["__interfaces"] != null && parent.constructor["__interfaces"].indexOf("io.apicurio.datamodels.openapi.models.IOasParameterParent") >= 0)) || parent === null) && ((typeof paramName === 'string') || paramName === null) && ((typeof paramType === 'string') || paramType === null) && ((typeof description === 'string') || description === null) && ((newType != null && newType instanceof SimplifiedParameterType) || newType === null) && ((typeof override === 'boolean') || override === null)) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._paramName === undefined)
                _this._paramName = null;
            if (_this._paramType === undefined)
                _this._paramType = null;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._description === undefined)
                _this._description = null;
            if (_this._newType === undefined)
                _this._newType = null;
            if (_this._override === undefined)
                _this._override = false;
            if (_this._created === undefined)
                _this._created = false;
            if (_this._paramName === undefined)
                _this._paramName = null;
            if (_this._paramType === undefined)
                _this._paramType = null;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._description === undefined)
                _this._description = null;
            if (_this._newType === undefined)
                _this._newType = null;
            if (_this._override === undefined)
                _this._override = false;
            if (_this._created === undefined)
                _this._created = false;
            (function () {
                _this._parentPath = Library.createNodePath(parent);
                _this._paramName = paramName;
                _this._paramType = paramType;
                _this._description = description;
                _this._newType = newType;
                _this._override = override;
            })();
        }
        else if (parent === undefined && paramName === undefined && paramType === undefined && description === undefined && newType === undefined && override === undefined) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._paramName === undefined)
                _this._paramName = null;
            if (_this._paramType === undefined)
                _this._paramType = null;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._description === undefined)
                _this._description = null;
            if (_this._newType === undefined)
                _this._newType = null;
            if (_this._override === undefined)
                _this._override = false;
            if (_this._created === undefined)
                _this._created = false;
            if (_this._paramName === undefined)
                _this._paramName = null;
            if (_this._paramType === undefined)
                _this._paramType = null;
            if (_this._parentPath === undefined)
                _this._parentPath = null;
            if (_this._description === undefined)
                _this._description = null;
            if (_this._newType === undefined)
                _this._newType = null;
            if (_this._override === undefined)
                _this._override = false;
            if (_this._created === undefined)
                _this._created = 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
     */
    NewParamCommand.prototype.execute = function (document) {
        LoggerCompat.info("[NewParamCommand] Executing.");
        this._created = false;
        var parent = this._parentPath.resolve(document);
        if (this.isNullOrUndefined(parent)) {
            LoggerCompat.info("[NewParamCommand] Parent node (operation or path item) is null.");
            return;
        }
        if (this.hasParam(this._paramName, this._paramType, parent)) {
            LoggerCompat.info("[NewParamCommand] Param %s of type %s already exists.", this._paramName, this._paramType);
            return;
        }
        var parameters = parent.getParameters();
        if (this.isNullOrUndefined(parameters)) {
            parameters = ([]);
            NodeCompat.setProperty(parent, Constants.PROP_PARAMETERS, parameters);
        }
        var param = parent.createParameter();
        var configured = false;
        if (this._override) {
            var oparam = this.findOverridableParam(parent);
            if (ModelUtils.isDefined(oparam)) {
                Library.readNode(Library.writeNode(oparam), param);
                configured = true;
            }
        }
        if (!configured) {
            param.in = this._paramType;
            param.name = this._paramName;
            if (NodeCompat.equals(param.in, "path")) {
                param.required = true;
            }
            if (ModelUtils.isDefined(this._description)) {
                param.description = this._description;
            }
            if (ModelUtils.isDefined(this._newType)) {
                this._setParameterType(param);
            }
        }
        parent.addParameter(param);
        LoggerCompat.info("[NewParamCommand] Param %s of type %s created successfully.", param.name, param.in);
        this._created = true;
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    NewParamCommand.prototype.undo = function (document) {
        LoggerCompat.info("[NewParamCommand] Reverting.");
        if (!this._created) {
            return;
        }
        var parent = this._parentPath.resolve(document);
        if (this.isNullOrUndefined(parent)) {
            return;
        }
        var theParam = null;
        var parameters = parent.getParameters();
        for (var index196 = 0; index196 < parameters.length; index196++) {
            var param = parameters[index196];
            {
                if (NodeCompat.equals(param.in, this._paramType) && NodeCompat.equals(param.name, this._paramName)) {
                    theParam = param;
                    break;
                }
            }
        }
        if (ModelUtils.isDefined(theParam)) {
            /* remove */ parameters.splice(parameters.indexOf(theParam), 1)[0];
            if ( /* size */parameters.length === 0) {
                NodeCompat.setProperty(parent, Constants.PROP_PARAMETERS, null);
            }
        }
    };
    /**
     * Sets the parameter type.
     * @param {OasParameter} parameter
     */
    NewParamCommand.prototype._setParameterType = function (parameter) {
        if (parameter.ownerDocument().getDocumentType() === DocumentType.openapi2) {
            var param = parameter;
            SimplifiedTypeUtil.setSimplifiedTypeOnParam(param, this._newType);
        }
        else {
            var schema = parameter.createSchema();
            SimplifiedTypeUtil.setSimplifiedType(schema, this._newType);
            parameter.schema = schema;
        }
        var required = this._newType.required;
        if (NodeCompat.equals(parameter.in, "path")) {
            required = true;
        }
        if (ModelUtils.isDefined(required)) {
            parameter.required = required;
        }
    };
    /**
     * Returns true if the given param already exists in the parent.
     * @param {string} paramName
     * @param {string} paramType
     * @param {*} parent
     * @returns {boolean}
     * @return {boolean}
     * @private
     */
    /*private*/ NewParamCommand.prototype.hasParam = function (paramName, paramType, parent) {
        return ModelUtils.isDefined(parent.getParameter(paramType, paramName));
    };
    /**
     * Tries to find the parameter being overridden (if any).  Returns null if it can't
     * find something.
     * @param {OasOperation} operation
     * @return {OasParameter}
     */
    NewParamCommand.prototype.findOverridableParam = function (operation) {
        var rval = null;
        var pathItem = operation.parent();
        if (ModelUtils.isDefined(pathItem)) {
            rval = pathItem.getParameter(this._paramType, this._paramName);
        }
        return rval;
    };
    return NewParamCommand;
}(AbstractCommand));
export { NewParamCommand };
NewParamCommand["__class"] = "io.apicurio.datamodels.cmd.commands.NewParamCommand";
NewParamCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
//# sourceMappingURL=NewParamCommand.js.map