/**
 * @license
 * Copyright 2019 Red Hat
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { CommandFactory } from '../cmd/commands/CommandFactory';
import { NodePath } from '../core/models/NodePath';
import { SimplifiedType } from '../cmd/models/SimplifiedType';
var pathKeys = [
    "_mediaTypePath", "_responsePath", "_responsesPath", "_parentPath", "_schemaPath", "_parameterPath", "_operationPath",
    "_propPath", "_propertyPath", "_paramPath", "_nodePath"
];
var pathListKeys = ["_references"];
var typeKeys = ["_newType"];
var cmdListKeys = ["_commands"];
var MarshallCompat = /** @class */ (function () {
    function MarshallCompat() {
    }
    MarshallCompat.marshallCommand = function (command) {
        var to = {};
        to["__type"] = command.type();
        Object.keys(command).filter(function (key) { return key != "__type"; }).forEach(function (key) {
            var value = command[key];
            if (value) {
                if (pathKeys.indexOf(key) != -1) {
                    value = value.toString();
                }
                else if (pathListKeys.indexOf(key) != -1 && value) {
                    var newValue_1 = [];
                    value.forEach(function (nodePath) {
                        newValue_1.push(nodePath.toString());
                    });
                    value = newValue_1;
                }
                else if (typeKeys.indexOf(key) != -1 && value) {
                    value = MarshallCompat.marshallSimplifiedType(value);
                }
                else if (cmdListKeys.indexOf(key) != -1 && value) {
                    var newValue_2 = [];
                    value.forEach(function (scmd) {
                        newValue_2.push(MarshallCompat.marshallCommand(scmd));
                    });
                    value = newValue_2;
                }
            }
            to[key] = value;
        });
        return to;
    };
    MarshallCompat.unmarshallCommand = function (from) {
        var type = from["__type"];
        if (!type) {
            throw Error("Missing __type from source data.");
        }
        var command = CommandFactory.create(type);
        Object.keys(from).filter(function (key) { return key != "__type"; }).forEach(function (key) {
            var value = from[key];
            if (value) {
                if (pathKeys.indexOf(key) != -1) {
                    value = new NodePath(value);
                }
                else if (pathListKeys.indexOf(key) != -1 && value) {
                    var newValue_3 = [];
                    value.forEach(function (np) {
                        newValue_3.push(new NodePath(np));
                    });
                    value = newValue_3;
                }
                else if (typeKeys.indexOf(key) != -1 && value) {
                    value = MarshallCompat.unmarshallSimplifiedType(value);
                }
                else if (cmdListKeys.indexOf(key) != -1 && value) {
                    var newValue_4 = [];
                    value.forEach(function (mcmd) {
                        newValue_4.push(MarshallCompat.unmarshallCommand(mcmd));
                    });
                    value = newValue_4;
                }
            }
            command[key] = value;
        });
        return command;
    };
    /**
     * Marshalls the given simple type into a JS object.
     * @param {SimplifiedType} sType
     * @return {any}
     */
    MarshallCompat.marshallSimplifiedType = function (sType) {
        if (sType == null || sType == undefined) {
            return null;
        }
        var obj = {
            type: sType.type,
            enum: sType.enum_,
            of: MarshallCompat.marshallSimplifiedType(sType.of),
            as: sType.as,
            required: sType["required"]
        };
        return obj;
    };
    /**
     * Unmarshalls a simple type back into a JS object.
     * @param object
     * @return {SimplifiedType}
     */
    MarshallCompat.unmarshallSimplifiedType = function (object) {
        if (object == undefined || object == null) {
            return null;
        }
        var type = new SimplifiedType();
        type.type = object.type;
        type.enum_ = object.enum;
        type.of = MarshallCompat.unmarshallSimplifiedType(object.of);
        type.as = object.as;
        type["required"] = object["required"];
        return type;
    };
    return MarshallCompat;
}());
export { MarshallCompat };
//# sourceMappingURL=MarshallCompat.js.map