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';
import { Constants } from '../../core/Constants';
import { Schema } from '../../core/models/common/Schema';
/**
 * A command used to create a new schema property.
 * @author eric.wittmann@gmail.com
 * @extends AbstractCommand
 * @class
 */
var NewSchemaPropertyCommand = /** @class */ (function (_super) {
    __extends(NewSchemaPropertyCommand, _super);
    function NewSchemaPropertyCommand(schema, propertyName, description, newType) {
        var _this = this;
        if (((schema != null && schema instanceof Schema) || schema === null) && ((typeof propertyName === 'string') || propertyName === null) && ((typeof description === 'string') || description === null) && ((newType != null && newType instanceof SimplifiedPropertyType) || newType === null)) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._propertyName === undefined)
                _this._propertyName = null;
            if (_this._schemaPath === undefined)
                _this._schemaPath = null;
            if (_this._description === undefined)
                _this._description = null;
            if (_this._newType === undefined)
                _this._newType = null;
            if (_this._created === undefined)
                _this._created = false;
            if (_this._nullRequired === undefined)
                _this._nullRequired = false;
            if (_this._propertyName === undefined)
                _this._propertyName = null;
            if (_this._schemaPath === undefined)
                _this._schemaPath = null;
            if (_this._description === undefined)
                _this._description = null;
            if (_this._newType === undefined)
                _this._newType = null;
            if (_this._created === undefined)
                _this._created = false;
            if (_this._nullRequired === undefined)
                _this._nullRequired = false;
            (function () {
                _this._schemaPath = Library.createNodePath(schema);
                _this._propertyName = propertyName;
                _this._description = description;
                _this._newType = newType;
            })();
        }
        else if (schema === undefined && propertyName === undefined && description === undefined && newType === undefined) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._propertyName === undefined)
                _this._propertyName = null;
            if (_this._schemaPath === undefined)
                _this._schemaPath = null;
            if (_this._description === undefined)
                _this._description = null;
            if (_this._newType === undefined)
                _this._newType = null;
            if (_this._created === undefined)
                _this._created = false;
            if (_this._nullRequired === undefined)
                _this._nullRequired = false;
            if (_this._propertyName === undefined)
                _this._propertyName = null;
            if (_this._schemaPath === undefined)
                _this._schemaPath = null;
            if (_this._description === undefined)
                _this._description = null;
            if (_this._newType === undefined)
                _this._newType = null;
            if (_this._created === undefined)
                _this._created = 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
     */
    NewSchemaPropertyCommand.prototype.execute = function (document) {
        LoggerCompat.info("[NewSchemaPropertyCommand] Executing.");
        this._created = false;
        var schema = this._schemaPath.resolve(document);
        if (this.isNullOrUndefined(schema)) {
            LoggerCompat.info("[NewSchemaPropertyCommand] Schema is null.");
            return;
        }
        if (ModelUtils.isDefined(schema.getProperty(this._propertyName))) {
            LoggerCompat.info("[NewSchemaPropertyCommand] Property already exists.");
            return;
        }
        var property = schema.createPropertySchema(this._propertyName);
        if (ModelUtils.isDefined(this._description)) {
            property.description = this._description;
        }
        if (ModelUtils.isDefined(this._newType)) {
            this._setPropertyType(property);
        }
        schema.addProperty(this._propertyName, property);
        LoggerCompat.info("[NewSchemaPropertyCommand] Property [%s] created successfully.", this._propertyName);
        this._created = true;
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    NewSchemaPropertyCommand.prototype.undo = function (document) {
        LoggerCompat.info("[NewSchemaPropertyCommand] Reverting.");
        if (!this._created) {
            return;
        }
        var schema = this._schemaPath.resolve(document);
        if (this.isNullOrUndefined(schema)) {
            return;
        }
        if (this.isNullOrUndefined(schema.getProperty(this._propertyName))) {
            return;
        }
        schema.removeProperty(this._propertyName);
        if (ModelUtils.isDefined(this._newType) && this._newType.required === true) {
            var required = schema.required;
            /* remove */ required.splice(required.indexOf(this._propertyName), 1)[0];
        }
    };
    /**
     * Sets the property type.
     * @param {*} prop
     */
    NewSchemaPropertyCommand.prototype._setPropertyType = function (prop) {
        SimplifiedTypeUtil.setSimplifiedType(prop, this._newType);
        if (ModelUtils.isDefined(this._newType) && this._newType.required === true) {
            var parent_1 = prop.parent();
            var required = parent_1.required;
            if (this.isNullOrUndefined(required)) {
                required = ([]);
                NodeCompat.setProperty(parent_1, Constants.PROP_REQUIRED, required);
                this._nullRequired = true;
            }
            /* add */ (required.push(prop.getPropertyName()) > 0);
        }
    };
    return NewSchemaPropertyCommand;
}(AbstractCommand));
export { NewSchemaPropertyCommand };
NewSchemaPropertyCommand["__class"] = "io.apicurio.datamodels.cmd.commands.NewSchemaPropertyCommand";
NewSchemaPropertyCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
//# sourceMappingURL=NewSchemaPropertyCommand.js.map