"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateLambdaDeleteRequestTemplate = exports.generateLambdaUpdateRequestTemplate = exports.generateUpdateInitSlotTemplate = exports.generateLambdaCreateRequestTemplate = exports.generateCreateInitSlotTemplate = void 0;
const graphql_mapping_template_1 = require("graphql-mapping-template");
const common_1 = require("../common");
const resolver_1 = require("./resolver");
const generateCreateInitSlotTemplate = (modelConfig, initializeIdField) => {
    const statements = [
        (0, graphql_mapping_template_1.qref)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('ctx.stash.put'), (0, graphql_mapping_template_1.str)('defaultValues'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.defaultIfNull'), (0, graphql_mapping_template_1.ref)('ctx.stash.defaultValues'), (0, graphql_mapping_template_1.obj)({})))),
    ];
    if (initializeIdField) {
        statements.push((0, common_1.defaultAutoId)());
    }
    if (modelConfig === null || modelConfig === void 0 ? void 0 : modelConfig.timestamps) {
        statements.push((0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('createdAt'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.time.nowISO8601'))));
        if (modelConfig.timestamps.createdAt) {
            statements.push((0, graphql_mapping_template_1.qref)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('ctx.stash.defaultValues.put'), (0, graphql_mapping_template_1.str)(modelConfig.timestamps.createdAt), (0, graphql_mapping_template_1.ref)('createdAt'))));
        }
        if (modelConfig.timestamps.updatedAt) {
            statements.push((0, graphql_mapping_template_1.qref)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('ctx.stash.defaultValues.put'), (0, graphql_mapping_template_1.str)(modelConfig.timestamps.updatedAt), (0, graphql_mapping_template_1.ref)('createdAt'))));
        }
    }
    statements.push((0, graphql_mapping_template_1.toJson)((0, graphql_mapping_template_1.obj)({
        version: (0, graphql_mapping_template_1.str)('2018-05-29'),
        payload: (0, graphql_mapping_template_1.obj)({}),
    })));
    return (0, graphql_mapping_template_1.printBlock)('Initialization default values')((0, graphql_mapping_template_1.compoundExpression)(statements));
};
exports.generateCreateInitSlotTemplate = generateCreateInitSlotTemplate;
const generateLambdaCreateRequestTemplate = (tableName, operationName, ctx) => {
    const mappedTableName = ctx.resourceHelper.getModelNameMapping(tableName);
    return (0, graphql_mapping_template_1.printBlock)('Invoke RDS Lambda data source')((0, graphql_mapping_template_1.compoundExpression)([
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput'), (0, graphql_mapping_template_1.obj)({})),
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput.table'), (0, graphql_mapping_template_1.str)(mappedTableName)),
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput.args'), (0, graphql_mapping_template_1.obj)({})),
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput.operation'), (0, graphql_mapping_template_1.str)('CREATE')),
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput.operationName'), (0, graphql_mapping_template_1.str)(operationName)),
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput.args.metadata'), (0, graphql_mapping_template_1.obj)({})),
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput.args.metadata.keys'), (0, graphql_mapping_template_1.list)([])),
        (0, resolver_1.constructNonScalarFieldsStatement)(tableName, ctx),
        (0, resolver_1.constructArrayFieldsStatement)(tableName, ctx),
        (0, resolver_1.constructFieldMappingInput)(),
        (0, graphql_mapping_template_1.qref)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('lambdaInput.args.metadata.keys.addAll'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.defaultIfNull'), (0, graphql_mapping_template_1.ref)('ctx.stash.keys'), (0, graphql_mapping_template_1.list)([])))),
        (0, graphql_mapping_template_1.comment)('Set the default values to put request'),
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput.args.input'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.defaultIfNull'), (0, graphql_mapping_template_1.ref)('ctx.stash.defaultValues'), (0, graphql_mapping_template_1.obj)({}))),
        (0, graphql_mapping_template_1.comment)('copy the values from input'),
        (0, graphql_mapping_template_1.qref)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('lambdaInput.args.input.putAll'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.defaultIfNull'), (0, graphql_mapping_template_1.ref)('context.arguments.input'), (0, graphql_mapping_template_1.obj)({})))),
        (0, graphql_mapping_template_1.obj)({
            version: (0, graphql_mapping_template_1.str)('2018-05-29'),
            operation: (0, graphql_mapping_template_1.str)('Invoke'),
            payload: (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.toJson'), (0, graphql_mapping_template_1.ref)('lambdaInput')),
        }),
    ]));
};
exports.generateLambdaCreateRequestTemplate = generateLambdaCreateRequestTemplate;
const generateUpdateInitSlotTemplate = (modelConfig) => {
    const statements = [
        (0, graphql_mapping_template_1.qref)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('ctx.stash.put'), (0, graphql_mapping_template_1.str)('defaultValues'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.defaultIfNull'), (0, graphql_mapping_template_1.ref)('ctx.stash.defaultValues'), (0, graphql_mapping_template_1.obj)({})))),
    ];
    if (modelConfig === null || modelConfig === void 0 ? void 0 : modelConfig.timestamps) {
        if (modelConfig.timestamps.updatedAt) {
            statements.push((0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('updatedAt'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.time.nowISO8601'))));
            statements.push((0, graphql_mapping_template_1.qref)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('ctx.stash.defaultValues.put'), (0, graphql_mapping_template_1.str)(modelConfig.timestamps.updatedAt), (0, graphql_mapping_template_1.ref)('updatedAt'))));
        }
    }
    statements.push((0, graphql_mapping_template_1.toJson)((0, graphql_mapping_template_1.obj)({
        version: (0, graphql_mapping_template_1.str)('2018-05-29'),
        payload: (0, graphql_mapping_template_1.obj)({}),
    })));
    return (0, graphql_mapping_template_1.printBlock)('Initialization default values')((0, graphql_mapping_template_1.compoundExpression)(statements));
};
exports.generateUpdateInitSlotTemplate = generateUpdateInitSlotTemplate;
const generateLambdaUpdateRequestTemplate = (tableName, operationName, modelIndexFields, ctx) => {
    const mappedTableName = ctx.resourceHelper.getModelNameMapping(tableName);
    return (0, graphql_mapping_template_1.printBlock)('Invoke RDS Lambda data source')((0, graphql_mapping_template_1.compoundExpression)([
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput'), (0, graphql_mapping_template_1.obj)({})),
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput.table'), (0, graphql_mapping_template_1.str)(mappedTableName)),
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput.args'), (0, graphql_mapping_template_1.obj)({})),
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput.operation'), (0, graphql_mapping_template_1.str)('UPDATE')),
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput.operationName'), (0, graphql_mapping_template_1.str)(operationName)),
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput.args.metadata'), (0, graphql_mapping_template_1.obj)({})),
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput.args.metadata.keys'), (0, graphql_mapping_template_1.list)([])),
        (0, resolver_1.constructNonScalarFieldsStatement)(tableName, ctx),
        (0, resolver_1.constructArrayFieldsStatement)(tableName, ctx),
        (0, resolver_1.constructFieldMappingInput)(),
        (0, graphql_mapping_template_1.qref)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('lambdaInput.args.metadata.keys.addAll'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.defaultIfNull'), (0, graphql_mapping_template_1.ref)('ctx.stash.keys'), (0, graphql_mapping_template_1.list)([])))),
        (0, graphql_mapping_template_1.comment)('Set the default values to put request'),
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput.args.input'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.defaultIfNull'), (0, graphql_mapping_template_1.ref)('ctx.stash.defaultValues'), (0, graphql_mapping_template_1.obj)({}))),
        (0, graphql_mapping_template_1.comment)('copy the values from input'),
        (0, graphql_mapping_template_1.qref)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('lambdaInput.args.input.putAll'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.defaultIfNull'), (0, graphql_mapping_template_1.ref)('context.arguments.input'), (0, graphql_mapping_template_1.obj)({})))),
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput.args.condition'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.defaultIfNull'), (0, graphql_mapping_template_1.ref)('context.arguments.condition'), (0, graphql_mapping_template_1.obj)({}))),
        (0, graphql_mapping_template_1.obj)({
            version: (0, graphql_mapping_template_1.str)('2018-05-29'),
            operation: (0, graphql_mapping_template_1.str)('Invoke'),
            payload: (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.toJson'), (0, graphql_mapping_template_1.ref)('lambdaInput')),
        }),
    ]));
};
exports.generateLambdaUpdateRequestTemplate = generateLambdaUpdateRequestTemplate;
const generateLambdaDeleteRequestTemplate = (tableName, operationName, modelIndexFields, ctx) => {
    const mappedTableName = ctx.resourceHelper.getModelNameMapping(tableName);
    return (0, graphql_mapping_template_1.printBlock)('Invoke RDS Lambda data source')((0, graphql_mapping_template_1.compoundExpression)([
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput'), (0, graphql_mapping_template_1.obj)({})),
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput.table'), (0, graphql_mapping_template_1.str)(mappedTableName)),
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput.args'), (0, graphql_mapping_template_1.ref)('context.arguments')),
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput.operation'), (0, graphql_mapping_template_1.str)('DELETE')),
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput.operationName'), (0, graphql_mapping_template_1.str)(operationName)),
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput.args.metadata'), (0, graphql_mapping_template_1.obj)({})),
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput.args.metadata.keys'), (0, graphql_mapping_template_1.list)([])),
        (0, resolver_1.constructNonScalarFieldsStatement)(tableName, ctx),
        (0, resolver_1.constructArrayFieldsStatement)(tableName, ctx),
        (0, resolver_1.constructFieldMappingInput)(),
        (0, graphql_mapping_template_1.qref)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('lambdaInput.args.metadata.keys.addAll'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.defaultIfNull'), (0, graphql_mapping_template_1.ref)('ctx.stash.keys'), (0, graphql_mapping_template_1.list)([])))),
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput.args.condition'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.defaultIfNull'), (0, graphql_mapping_template_1.ref)('context.arguments.condition'), (0, graphql_mapping_template_1.obj)({}))),
        (0, graphql_mapping_template_1.obj)({
            version: (0, graphql_mapping_template_1.str)('2018-05-29'),
            operation: (0, graphql_mapping_template_1.str)('Invoke'),
            payload: (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.toJson'), (0, graphql_mapping_template_1.ref)('lambdaInput')),
        }),
    ]));
};
exports.generateLambdaDeleteRequestTemplate = generateLambdaDeleteRequestTemplate;
//# sourceMappingURL=mutation.js.map