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 { ModelUtils } from '../util/ModelUtils';
import { LoggerCompat } from '../../compat/LoggerCompat';
import { OasSchemaFactory } from '../../core/factories/OasSchemaFactory';
import { NewSchemaDefinitionCommand } from './NewSchemaDefinitionCommand';
/**
 * @author eric.wittmann@gmail.com
 * @extends NewSchemaDefinitionCommand
 * @class
 */
var NewSchemaDefinitionCommand_20 = /** @class */ (function (_super) {
    __extends(NewSchemaDefinitionCommand_20, _super);
    function NewSchemaDefinitionCommand_20(definitionName, example, description) {
        var _this = this;
        if (((typeof definitionName === 'string') || definitionName === null) && ((example != null) || example === null) && ((typeof description === 'string') || description === null)) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._nullDefinitions === undefined)
                _this._nullDefinitions = false;
            if (_this._nullDefinitions === undefined)
                _this._nullDefinitions = false;
            (function () {
                _this._newDefinitionName = definitionName;
                _this._newDefinitionExample = example;
                _this._newDefinitionDescription = description;
            })();
        }
        else if (definitionName === undefined && example === undefined && description === undefined) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._nullDefinitions === undefined)
                _this._nullDefinitions = false;
            if (_this._nullDefinitions === undefined)
                _this._nullDefinitions = 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
     */
    NewSchemaDefinitionCommand_20.prototype.execute = function (document) {
        LoggerCompat.info("[NewDefinitionCommand] Executing.");
        var doc20 = document;
        if (this.isNullOrUndefined(doc20.definitions)) {
            doc20.definitions = doc20.createDefinitions();
            this._nullDefinitions = true;
        }
        if (this.isNullOrUndefined(doc20.definitions.getDefinition(this._newDefinitionName))) {
            var definition = void 0;
            if (!this.isNullOrUndefined(this._newDefinitionExample)) {
                definition = OasSchemaFactory.createSchemaDefinitionFromExample(doc20, this._newDefinitionName, this._newDefinitionExample);
                definition.example = this._newDefinitionExample;
            }
            else {
                definition = doc20.definitions.createSchemaDefinition(this._newDefinitionName);
                definition.type = "object";
            }
            if (ModelUtils.isDefined(this._newDefinitionDescription)) {
                definition.description = this._newDefinitionDescription;
            }
            doc20.definitions.addDefinition(this._newDefinitionName, definition);
            this._defExisted = false;
        }
        else {
            this._defExisted = true;
        }
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    NewSchemaDefinitionCommand_20.prototype.undo = function (document) {
        LoggerCompat.info("[NewDefinitionCommand] Reverting.");
        var doc20 = document;
        if (this._nullDefinitions) {
            doc20.definitions = null;
        }
        if (this._defExisted) {
            return;
        }
        doc20.definitions.removeDefinition(this._newDefinitionName);
    };
    return NewSchemaDefinitionCommand_20;
}(NewSchemaDefinitionCommand));
export { NewSchemaDefinitionCommand_20 };
NewSchemaDefinitionCommand_20["__class"] = "io.apicurio.datamodels.cmd.commands.NewSchemaDefinitionCommand_20";
NewSchemaDefinitionCommand_20["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
//# sourceMappingURL=NewSchemaDefinitionCommand_20.js.map