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 { AbstractCommand } from '../AbstractCommand';
import { ModelUtils } from '../util/ModelUtils';
import { LoggerCompat } from '../../compat/LoggerCompat';
import { NodeCompat } from '../../compat/NodeCompat';
/**
 * A command used to rename a path item, along with all references to it.
 * @author eric.wittmann@gmail.com
 * @extends AbstractCommand
 * @class
 */
var RenamePathItemCommand = /** @class */ (function (_super) {
    __extends(RenamePathItemCommand, _super);
    function RenamePathItemCommand(oldPath, newPath, alsoRenameSubpaths) {
        var _this = this;
        if (((typeof oldPath === 'string') || oldPath === null) && ((typeof newPath === 'string') || newPath === null) && ((typeof alsoRenameSubpaths === 'boolean') || alsoRenameSubpaths === null)) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._oldPath === undefined)
                _this._oldPath = null;
            if (_this._newPath === undefined)
                _this._newPath = null;
            if (_this._alsoRenameSubpaths === undefined)
                _this._alsoRenameSubpaths = false;
            if (_this._oldPath === undefined)
                _this._oldPath = null;
            if (_this._newPath === undefined)
                _this._newPath = null;
            if (_this._alsoRenameSubpaths === undefined)
                _this._alsoRenameSubpaths = false;
            (function () {
                _this._oldPath = oldPath;
                _this._newPath = newPath;
                _this._alsoRenameSubpaths = alsoRenameSubpaths;
            })();
        }
        else if (oldPath === undefined && newPath === undefined && alsoRenameSubpaths === undefined) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._oldPath === undefined)
                _this._oldPath = null;
            if (_this._newPath === undefined)
                _this._newPath = null;
            if (_this._alsoRenameSubpaths === undefined)
                _this._alsoRenameSubpaths = false;
            if (_this._oldPath === undefined)
                _this._oldPath = null;
            if (_this._newPath === undefined)
                _this._newPath = null;
            if (_this._alsoRenameSubpaths === undefined)
                _this._alsoRenameSubpaths = 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
     */
    RenamePathItemCommand.prototype.execute = function (document) {
        LoggerCompat.info("[RenamePathItemCommand] Executing.");
        this._doPathRename(document, this._oldPath, this._newPath, this._alsoRenameSubpaths);
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    RenamePathItemCommand.prototype.undo = function (document) {
        LoggerCompat.info("[RenamePathItemCommand] Reverting.");
        this._doPathRename(document, this._newPath, this._oldPath, this._alsoRenameSubpaths);
    };
    /**
     * Does the work of renaming a path from one name to another.
     * @param {Document} document
     * @param {string} from
     * @param {string} to
     * @param {boolean} alsoRenameSubpaths
     * @private
     */
    /*private*/ RenamePathItemCommand.prototype._doPathRename = function (document, from, to, alsoRenameSubpaths) {
        var _this = this;
        var pathsToRename = ([]);
        /* add */ (pathsToRename.push([from, to]) > 0);
        var odoc = document;
        if (this._alsoRenameSubpaths && ModelUtils.isDefined(odoc.paths)) {
            var pathItemNames = odoc.paths.getPathItemNames();
            for (var index229 = 0; index229 < pathItemNames.length; index229++) {
                var pathName = pathItemNames[index229];
                {
                    if (pathName.indexOf(from) === 0 && !NodeCompat.equals(pathName, from)) {
                        /* add */ (pathsToRename.push([pathName, to + pathName.substring(from.length)]) > 0);
                    }
                }
            }
        }
        pathsToRename.forEach((function (odoc) {
            return function (p2r) {
                _this._renamePath(odoc, p2r[0], p2r[1]);
            };
        })(odoc));
    };
    /**
     * Does the work of renaming a path.
     * @param {string} from
     * @param {string} to
     * @param {OasDocument} document
     * @private
     */
    /*private*/ RenamePathItemCommand.prototype._renamePath = function (document, from, to) {
        var fromPathParamNames = ModelUtils.detectPathParamNames(from);
        var toPathParamNames = ModelUtils.detectPathParamNames(to);
        if ( /* size */fromPathParamNames.length !== /* size */ toPathParamNames.length) {
            LoggerCompat.warn("Renaming a path with %o parameters to a new value that has %o parameters!", /* size */ fromPathParamNames.length, /* size */ toPathParamNames.length);
        }
        var path = document.paths.removePathItem(from);
        if (NodeCompat.isNullOrUndefined(path)) {
            return;
        }
        path.rename(to);
        document.paths.addPathItem(to, path);
        for (var idx = 0; idx < /* size */ fromPathParamNames.length; idx++) {
            {
                var fromParamName = fromPathParamNames[idx];
                var toParamName = null;
                if (idx < /* size */ toPathParamNames.length) {
                    toParamName = /* get */ toPathParamNames[idx];
                }
                if (ModelUtils.isDefined(toParamName)) {
                    this._renamePathParameter(path, fromParamName, toParamName);
                }
                else {
                    this._removePathParameter(path, fromParamName);
                }
            }
            ;
        }
        if ( /* size */toPathParamNames.length > /* size */ fromPathParamNames.length) {
            for (var idx = fromPathParamNames.length; idx < /* size */ toPathParamNames.length; idx++) {
                {
                    var paramName = toPathParamNames[idx];
                    var parameter = path.createParameter();
                    parameter.name = paramName;
                    parameter.in = "path";
                    parameter.required = true;
                    path.addParameter(parameter);
                }
                ;
            }
        }
    };
    /**
     * Rename a path parameter.
     * @param {OasPathItem} path
     * @param {string} fromParamName
     * @param {string} toParamName
     * @private
     */
    /*private*/ RenamePathItemCommand.prototype._renamePathParameter = function (path, fromParamName, toParamName) {
        if (!NodeCompat.equals(fromParamName, toParamName) && ModelUtils.isDefined(path.parameters)) {
            path.parameters.forEach(function (param) {
                if (NodeCompat.equals(param.in, "path") && NodeCompat.equals(param.name, fromParamName)) {
                    param.name = toParamName;
                }
            });
        }
    };
    /**
     * Remove a path parameter.
     * @param {OasPathItem} path
     * @param {string} fromParamName
     * @private
     */
    /*private*/ RenamePathItemCommand.prototype._removePathParameter = function (path, fromParamName) {
        if (this.isNullOrUndefined(path.parameters)) {
            return;
        }
        var paramIdx = -1;
        for (var idx = 0; idx < /* size */ path.parameters.length; idx++) {
            {
                var param = path.parameters[idx];
                if (NodeCompat.equals(param.name, fromParamName) && NodeCompat.equals(param.in, "path")) {
                    paramIdx = idx;
                }
            }
            ;
        }
        if (paramIdx !== -1) {
            /* remove */ path.parameters.splice(paramIdx, 1)[0];
        }
    };
    return RenamePathItemCommand;
}(AbstractCommand));
export { RenamePathItemCommand };
RenamePathItemCommand["__class"] = "io.apicurio.datamodels.cmd.commands.RenamePathItemCommand";
RenamePathItemCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
//# sourceMappingURL=RenamePathItemCommand.js.map