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 { LoggerCompat } from '../../compat/LoggerCompat';
import { NodeCompat } from '../../compat/NodeCompat';
import { OasSchema } from '../../openapi/models/OasSchema';
import { AbstractSchemaInhCommand } from './AbstractSchemaInhCommand';
/**
 * A command used to add a child schema to the
 * @author eric.wittmann@gmail.com
 * @extends AbstractSchemaInhCommand
 * @class
 */
var AddChildSchemaCommand = /** @class */ (function (_super) {
    __extends(AddChildSchemaCommand, _super);
    function AddChildSchemaCommand(schema, childSchema, childType) {
        var _this = this;
        if (((schema != null && schema instanceof OasSchema) || schema === null) && ((childSchema != null && childSchema instanceof OasSchema) || childSchema === null) && ((typeof childType === 'string') || childType === null)) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._schemaPath === undefined)
                _this._schemaPath = null;
            if (_this._newChildSchemaType === undefined)
                _this._newChildSchemaType = null;
            if (_this._newChildSchemaObj === undefined)
                _this._newChildSchemaObj = null;
            if (_this._nullChildList === undefined)
                _this._nullChildList = false;
            if (_this._childIndex === undefined)
                _this._childIndex = 0;
            if (_this._schemaPath === undefined)
                _this._schemaPath = null;
            if (_this._newChildSchemaType === undefined)
                _this._newChildSchemaType = null;
            if (_this._newChildSchemaObj === undefined)
                _this._newChildSchemaObj = null;
            if (_this._nullChildList === undefined)
                _this._nullChildList = false;
            if (_this._childIndex === undefined)
                _this._childIndex = 0;
            (function () {
                _this._schemaPath = Library.createNodePath(schema);
                _this._newChildSchemaObj = Library.writeNode(childSchema);
                _this._newChildSchemaType = childType;
            })();
        }
        else if (schema === undefined && childSchema === undefined && childType === undefined) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._schemaPath === undefined)
                _this._schemaPath = null;
            if (_this._newChildSchemaType === undefined)
                _this._newChildSchemaType = null;
            if (_this._newChildSchemaObj === undefined)
                _this._newChildSchemaObj = null;
            if (_this._nullChildList === undefined)
                _this._nullChildList = false;
            if (_this._childIndex === undefined)
                _this._childIndex = 0;
            if (_this._schemaPath === undefined)
                _this._schemaPath = null;
            if (_this._newChildSchemaType === undefined)
                _this._newChildSchemaType = null;
            if (_this._newChildSchemaObj === undefined)
                _this._newChildSchemaObj = null;
            if (_this._nullChildList === undefined)
                _this._nullChildList = false;
            if (_this._childIndex === undefined)
                _this._childIndex = 0;
        }
        else
            throw new Error('invalid overload');
        return _this;
    }
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#execute(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    AddChildSchemaCommand.prototype.execute = function (document) {
        LoggerCompat.info("[AddChildSchemaCommand] Executing.");
        this._nullChildList = false;
        this._childIndex = -1;
        var schema = this._schemaPath.resolve(document);
        if (this.isNullOrUndefined(schema)) {
            return;
        }
        var schemas = null;
        var newSchema = null;
        if (NodeCompat.equals(AbstractSchemaInhCommand.TYPE_ALL_OF, this._newChildSchemaType)) {
            schemas = schema.allOf;
            newSchema = schema.createAllOfSchema();
            Library.readNode(this._newChildSchemaObj, newSchema);
            schema.addAllOfSchema(newSchema);
            this._childIndex = /* size */ schema.allOf.length - 1;
        }
        if (NodeCompat.equals(AbstractSchemaInhCommand.TYPE_ANY_OF, this._newChildSchemaType)) {
            var schema30 = schema;
            schemas = schema30.anyOf;
            newSchema = schema.createAnyOfSchema();
            Library.readNode(this._newChildSchemaObj, newSchema);
            schema.addAnyOfSchema(newSchema);
            this._childIndex = /* size */ schema.anyOf.length - 1;
        }
        if (NodeCompat.equals(AbstractSchemaInhCommand.TYPE_ONE_OF, this._newChildSchemaType)) {
            var schema30 = schema;
            schemas = schema30.oneOf;
            newSchema = schema.createOneOfSchema();
            Library.readNode(this._newChildSchemaObj, newSchema);
            schema.addOneOfSchema(newSchema);
            this._childIndex = /* size */ schema.oneOf.length - 1;
        }
        if (schemas == null) {
            this._nullChildList = true;
        }
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    AddChildSchemaCommand.prototype.undo = function (document) {
        LoggerCompat.info("[AddChildSchemaCommand] Reverting.");
        var schema = this._schemaPath.resolve(document);
        if (this.isNullOrUndefined(schema) || this._childIndex === -1) {
            return;
        }
        if (NodeCompat.equals(AbstractSchemaInhCommand.TYPE_ALL_OF, this._newChildSchemaType)) {
            if (this._nullChildList) {
                schema.allOf = null;
            }
            else {
                /* remove */ schema.allOf.splice(this._childIndex, 1)[0];
            }
        }
        if (NodeCompat.equals(AbstractSchemaInhCommand.TYPE_ANY_OF, this._newChildSchemaType)) {
            var schema30 = schema;
            if (this._nullChildList) {
                schema30.anyOf = null;
            }
            else {
                /* remove */ schema30.anyOf.splice(this._childIndex, 1)[0];
            }
        }
        if (NodeCompat.equals(AbstractSchemaInhCommand.TYPE_ONE_OF, this._newChildSchemaType)) {
            var schema30 = schema;
            if (this._nullChildList) {
                schema30.oneOf = null;
            }
            else {
                /* remove */ schema30.oneOf.splice(this._childIndex, 1)[0];
            }
        }
    };
    return AddChildSchemaCommand;
}(AbstractSchemaInhCommand));
export { AddChildSchemaCommand };
AddChildSchemaCommand["__class"] = "io.apicurio.datamodels.cmd.commands.AddChildSchemaCommand";
AddChildSchemaCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
//# sourceMappingURL=AddChildSchemaCommand.js.map