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 { SimplifiedPropertyType } from '../models/SimplifiedPropertyType';
import { ModelUtils } from '../util/ModelUtils';
import { SimplifiedTypeUtil } from '../util/SimplifiedTypeUtil';
import { LoggerCompat } from '../../compat/LoggerCompat';
import { NodeCompat } from '../../compat/NodeCompat';
/**
 * A command used to modify the type of a property of a schema.
 * @author eric.wittmann@gmail.com
 * @extends AbstractCommand
 * @class
 */
var ChangePropertyTypeCommand = /** @class */ (function (_super) {
    __extends(ChangePropertyTypeCommand, _super);
    function ChangePropertyTypeCommand(property, newType) {
        var _this = this;
        if (((property != null && (property["__interfaces"] != null && property["__interfaces"].indexOf("io.apicurio.datamodels.openapi.models.IOasPropertySchema") >= 0 || property.constructor != null && property.constructor["__interfaces"] != null && property.constructor["__interfaces"].indexOf("io.apicurio.datamodels.openapi.models.IOasPropertySchema") >= 0)) || property === null) && ((newType != null && newType instanceof SimplifiedPropertyType) || newType === null)) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._propPath === undefined)
                _this._propPath = null;
            if (_this._propName === undefined)
                _this._propName = null;
            if (_this._newType === undefined)
                _this._newType = null;
            if (_this._oldProperty === undefined)
                _this._oldProperty = null;
            if (_this._oldRequired === undefined)
                _this._oldRequired = false;
            if (_this._nullRequired === undefined)
                _this._nullRequired = false;
            if (_this._propPath === undefined)
                _this._propPath = null;
            if (_this._propName === undefined)
                _this._propName = null;
            if (_this._newType === undefined)
                _this._newType = null;
            if (_this._oldProperty === undefined)
                _this._oldProperty = null;
            if (_this._oldRequired === undefined)
                _this._oldRequired = false;
            if (_this._nullRequired === undefined)
                _this._nullRequired = false;
            (function () {
                _this._propPath = Library.createNodePath(property);
                _this._propName = property.getPropertyName();
                _this._newType = newType;
            })();
        }
        else if (property === undefined && newType === undefined) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._propPath === undefined)
                _this._propPath = null;
            if (_this._propName === undefined)
                _this._propName = null;
            if (_this._newType === undefined)
                _this._newType = null;
            if (_this._oldProperty === undefined)
                _this._oldProperty = null;
            if (_this._oldRequired === undefined)
                _this._oldRequired = false;
            if (_this._nullRequired === undefined)
                _this._nullRequired = false;
            if (_this._propPath === undefined)
                _this._propPath = null;
            if (_this._propName === undefined)
                _this._propName = null;
            if (_this._newType === undefined)
                _this._newType = null;
            if (_this._oldProperty === undefined)
                _this._oldProperty = null;
            if (_this._oldRequired === undefined)
                _this._oldRequired = false;
            if (_this._nullRequired === undefined)
                _this._nullRequired = 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
     */
    ChangePropertyTypeCommand.prototype.execute = function (document) {
        LoggerCompat.info("[ChangePropertyTypeCommand] Executing: " + this._newType);
        var prop = this._propPath.resolve(document);
        if (this.isNullOrUndefined(prop)) {
            return;
        }
        var parent = prop.parent();
        var required = parent.required;
        this._oldProperty = Library.writeNode(prop);
        this._oldRequired = ModelUtils.isDefined(required) && /* size */ required.length > 0 && required.indexOf(prop.getPropertyName()) !== -1;
        SimplifiedTypeUtil.setSimplifiedType(prop, this._newType);
        if (!this.isNullOrUndefined(this._newType.required)) {
            if (this._newType.required && !this._oldRequired) {
                if (this.isNullOrUndefined(required)) {
                    required = ([]);
                    parent.required = required;
                    this._nullRequired = true;
                }
                /* add */ (required.push(prop.getPropertyName()) > 0);
            }
            if (!this._newType.required && this._oldRequired) {
                /* remove */ required.splice(required.indexOf(prop.getPropertyName()), 1)[0];
            }
        }
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    ChangePropertyTypeCommand.prototype.undo = function (document) {
        LoggerCompat.info("[ChangePropertyTypeCommand] Reverting.");
        var prop = this._propPath.resolve(document);
        if (this.isNullOrUndefined(prop)) {
            return;
        }
        var parent = prop.parent();
        var required = parent.required;
        var wasRequired = ModelUtils.isDefined(required) && /* size */ required.length > 0 && required.indexOf(prop.getPropertyName()) !== -1;
        var oldProp = parent.createPropertySchema(this._propName);
        Library.readNode(this._oldProperty, oldProp);
        var sprop = prop;
        sprop.$ref = null;
        sprop.type = null;
        sprop.enum_ = null;
        sprop.format = null;
        sprop.items = null;
        if (ModelUtils.isDefined(oldProp)) {
            sprop.$ref = oldProp.$ref;
            sprop.type = oldProp.type;
            sprop.enum_ = oldProp.enum_;
            sprop.format = oldProp.format;
            sprop.items = oldProp.items;
            if (ModelUtils.isDefined(sprop.items) && !NodeCompat.isList(sprop.items)) {
                var itemsNode = sprop.items;
                itemsNode._parent = sprop;
                itemsNode._ownerDocument = sprop.ownerDocument();
            }
        }
        if (!this.isNullOrUndefined(this._newType.required)) {
            if (this._nullRequired) {
                parent.required = null;
            }
            else {
                if (wasRequired && !this._oldRequired) {
                    /* remove */ required.splice(required.indexOf(prop.getPropertyName()), 1)[0];
                }
                if (!wasRequired && this._oldRequired) {
                    /* add */ (required.push(prop.getPropertyName()) > 0);
                }
            }
        }
    };
    return ChangePropertyTypeCommand;
}(AbstractCommand));
export { ChangePropertyTypeCommand };
ChangePropertyTypeCommand["__class"] = "io.apicurio.datamodels.cmd.commands.ChangePropertyTypeCommand";
ChangePropertyTypeCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
//# sourceMappingURL=ChangePropertyTypeCommand.js.map