Object.defineProperty(exports, "__esModule", { value: true });
const babelPluginHelpers_1 = require("../babelPluginHelpers");
const config_1 = require("../config");
const OperationTypes = require("../OperationTypes");
const OBJECT_METHOD = config_1.EXPLICIT_NAMES ? "ObjectMethod" : "met";
const OBJECT_PROPERTY = config_1.EXPLICIT_NAMES ? "ObjectProperty" : "pr";
exports.default = {
    argNames: ["property"],
    argIsArray: [true],
    exec: (args, astArgs, ctx, logData) => {
        const [propertiesArg] = args;
        var obj = {};
        var methodProperties = {};
        for (var i = 0; i < propertiesArg.length; i++) {
            var property = propertiesArg[i];
            const [type, key, value, kind] = property;
            var propertyType = type[0];
            var propertyKey = key[0];
            if (propertyType === OBJECT_PROPERTY) {
                var propertyValue = value[0];
                var propertyValueT = value[1];
                obj[propertyKey] = propertyValue;
                ctx.trackObjectPropertyAssignment(obj, propertyKey, ctx.createOperationLog({
                    operation: OperationTypes.objectProperty,
                    args: { propertyValue: [propertyValue, propertyValueT] },
                    result: propertyValue,
                    astArgs: {},
                    loc: logData.loc
                }), key[1]);
            }
            else if (propertyType === OBJECT_METHOD) {
                var propertyKind = kind[0];
                var fn = value[0];
                if (!methodProperties[propertyKey]) {
                    methodProperties[propertyKey] = {
                        enumerable: true,
                        configurable: true
                    };
                }
                if (propertyKind === "method") {
                    obj[propertyKey] = fn;
                }
                else {
                    methodProperties[propertyKey][propertyKind] = fn;
                }
            }
        }
        Object.defineProperties(obj, methodProperties);
        return obj;
    },
    traverse(operationLog, charIndex) {
        return {
            operationLog: operationLog.args.propertyValue,
            charIndex: charIndex
        };
    },
    visitor(path) {
        path.node.properties.forEach(function (prop) {
            if (prop.key.type === "Identifier") {
                const loc = prop.key.loc;
                prop.key = babelPluginHelpers_1.getBabelTypes().stringLiteral(prop.key.name);
                prop.key.loc = loc;
                if (!loc) {
                    debugger;
                }
            }
        });
        function makeArray(type, key, value, kind) {
            var ret = [type, key, value];
            if (kind !== undefined) {
                ret.push(kind);
            }
            ret = ret.map(a => babelPluginHelpers_1.ignoredArrayExpression(a));
            return babelPluginHelpers_1.ignoredArrayExpression(ret);
        }
        var properties = path.node.properties.map(function (prop) {
            if (prop.type === "ObjectMethod") {
                // getters/setters or something like this: obj = {fn(){}}
                var kind = babelPluginHelpers_1.ignoredStringLiteral(prop.kind);
                kind.ignore = true;
                return makeArray([babelPluginHelpers_1.ignoredStringLiteral(OBJECT_METHOD)], [prop.key], [babelPluginHelpers_1.getBabelTypes().functionExpression(null, prop.params, prop.body)], [kind]);
            }
            else {
                return makeArray([babelPluginHelpers_1.ignoredStringLiteral(OBJECT_PROPERTY)], [prop.key, babelPluginHelpers_1.getLastOperationTrackingResultCall()], [prop.value, babelPluginHelpers_1.getLastOperationTrackingResultCall()]);
            }
        });
        var call = this.createNode([properties], null, path.node.loc);
        return call;
    }
};
//# sourceMappingURL=ObjectExpression.js.map