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 { ModelUtils } from '../util/ModelUtils';
import { JsonCompat } from '../../compat/JsonCompat';
import { LoggerCompat } from '../../compat/LoggerCompat';
import { Constants } from '../../core/Constants';
import { OasSchema } from '../../openapi/models/OasSchema';
/**
 * A command used to delete all properties from a schema.
 * @author eric.wittmann@gmail.com
 * @extends AbstractCommand
 * @class
 */
var DeleteAllPropertiesCommand = /** @class */ (function (_super) {
    __extends(DeleteAllPropertiesCommand, _super);
    function DeleteAllPropertiesCommand(schema) {
        var _this = this;
        if (((schema != null && schema instanceof OasSchema) || schema === null)) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._schemaPath === undefined)
                _this._schemaPath = null;
            if (_this._oldProperties === undefined)
                _this._oldProperties = null;
            if (_this._schemaPath === undefined)
                _this._schemaPath = null;
            if (_this._oldProperties === undefined)
                _this._oldProperties = null;
            (function () {
                _this._schemaPath = Library.createNodePath(schema);
            })();
        }
        else if (schema === undefined) {
            var __args = arguments;
            _this = _super.call(this) || this;
            if (_this._schemaPath === undefined)
                _this._schemaPath = null;
            if (_this._oldProperties === undefined)
                _this._oldProperties = null;
            if (_this._schemaPath === undefined)
                _this._schemaPath = null;
            if (_this._oldProperties === undefined)
                _this._oldProperties = null;
        }
        else
            throw new Error('invalid overload');
        return _this;
    }
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#execute(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    DeleteAllPropertiesCommand.prototype.execute = function (document) {
        var _this = this;
        LoggerCompat.info("[DeleteAllPropertiesCommand] Executing.");
        this._oldProperties = ([]);
        var schema = this._schemaPath.resolve(document);
        if (this.isNullOrUndefined(schema)) {
            return;
        }
        schema.getPropertyNames().forEach((function (schema) {
            return function (pname) {
                var data = JsonCompat.objectNode();
                JsonCompat.setPropertyString(data, Constants.PROP_NAME, pname);
                JsonCompat.setProperty(data, Constants.PROP_SCHEMA, Library.writeNode(schema.removeProperty(pname)));
                JsonCompat.setPropertyBoolean(data, Constants.PROP_REQUIRED, _this.isPropertyRequired(schema, pname));
                /* add */ (_this._oldProperties.push(data) > 0);
            };
        })(schema));
        schema.required = null;
    };
    /**
     * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
     * @param {Document} document
     */
    DeleteAllPropertiesCommand.prototype.undo = function (document) {
        LoggerCompat.info("[DeleteAllPropertiesCommand] Reverting.");
        if (this.isNullOrUndefined(this._oldProperties) || /* size */ this._oldProperties.length === 0) {
            return;
        }
        var schema = this._schemaPath.resolve(document);
        if (this.isNullOrUndefined(schema)) {
            return;
        }
        if (this.isNullOrUndefined(schema.required)) {
            schema.required = ([]);
        }
        this._oldProperties.forEach((function (schema) {
            return function (oldProp) {
                var name = JsonCompat.getPropertyString(oldProp, Constants.PROP_NAME);
                var schemaObj = JsonCompat.getProperty(oldProp, Constants.PROP_SCHEMA);
                var required = JsonCompat.getPropertyBoolean(oldProp, Constants.PROP_REQUIRED);
                var prop = schema.createPropertySchema(name);
                Library.readNode(schemaObj, prop);
                schema.addProperty(name, prop);
                if (ModelUtils.isDefined(required) && required === true) {
                    /* add */ (schema.required.push(name) > 0);
                }
            };
        })(schema));
    };
    /**
     * Returns true if the property is required.
     * @param {OasSchema} schema
     * @param {string} propertyName
     * @return {boolean}
     * @private
     */
    /*private*/ DeleteAllPropertiesCommand.prototype.isPropertyRequired = function (schema, propertyName) {
        return ModelUtils.isDefined(schema.required) && schema.required.indexOf(propertyName) !== -1;
    };
    return DeleteAllPropertiesCommand;
}(AbstractCommand));
export { DeleteAllPropertiesCommand };
DeleteAllPropertiesCommand["__class"] = "io.apicurio.datamodels.cmd.commands.DeleteAllPropertiesCommand";
DeleteAllPropertiesCommand["__interfaces"] = ["io.apicurio.datamodels.cmd.ICommand"];
//# sourceMappingURL=DeleteAllPropertiesCommand.js.map