"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateAuthRequestExpression = exports.generateAuthExpressionForDelete = exports.generateAuthExpressionForUpdate = exports.generateAuthExpressionForCreate = void 0;
const graphql_mapping_template_1 = require("graphql-mapping-template");
const common_1 = require("./common");
const generateAuthExpressionForCreate = (ctx, providers, roles, fields) => {
    return generateMutationExpression(roles, fields, 'create', providers.hasIdentityPoolId, providers.genericIamAccessEnabled, false);
};
exports.generateAuthExpressionForCreate = generateAuthExpressionForCreate;
const generateAuthExpressionForUpdate = (providers, roles, fields) => {
    return generateMutationExpression(roles, fields, 'update', providers.hasIdentityPoolId, providers.genericIamAccessEnabled, true);
};
exports.generateAuthExpressionForUpdate = generateAuthExpressionForUpdate;
const generateAuthExpressionForDelete = (providers, roles, fields) => {
    return generateMutationExpression(roles, fields, 'delete', providers.hasIdentityPoolId, providers.genericIamAccessEnabled, true);
};
exports.generateAuthExpressionForDelete = generateAuthExpressionForDelete;
const generateMutationExpression = (roles, fields, operation, hasIdentityPoolId, enableIamAccess, includeExistingRecord = false) => {
    const expressions = [];
    expressions.push((0, graphql_mapping_template_1.compoundExpression)((0, common_1.generateAuthRulesFromRoles)(roles, fields, hasIdentityPoolId, false)));
    expressions.push((0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('authResult'), includeExistingRecord
        ? (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.authRules.mutationAuth'), (0, graphql_mapping_template_1.ref)('authRules'), (0, graphql_mapping_template_1.str)(operation), (0, graphql_mapping_template_1.ref)('ctx.args.input'), (0, graphql_mapping_template_1.ref)('ctx.result'))
        : (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.authRules.mutationAuth'), (0, graphql_mapping_template_1.ref)('authRules'), (0, graphql_mapping_template_1.str)(operation), (0, graphql_mapping_template_1.ref)('ctx.args.input'), (0, graphql_mapping_template_1.nul)())));
    expressions.push((0, common_1.validateAuthResult)(), (0, common_1.constructAuthorizedInputStatement)('ctx.args.input'), common_1.emptyPayload);
    return (0, graphql_mapping_template_1.printBlock)('Authorization rules')((0, common_1.generateIAMAccessCheck)(enableIamAccess, (0, graphql_mapping_template_1.compoundExpression)(expressions)));
};
const generateAuthRequestExpression = (ctx, def) => {
    const mappedTableName = ctx.resourceHelper.getModelNameMapping(def.name.value);
    const operation = 'GET';
    const operationName = 'GET_EXISTING_RECORD';
    return (0, graphql_mapping_template_1.printBlock)('Get existing record')((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.args'), (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.operation'), (0, graphql_mapping_template_1.str)(operation)),
        (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)([])),
        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.input'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.map.copyAndRetainAllKeys'), (0, graphql_mapping_template_1.ref)('context.arguments.input'), (0, graphql_mapping_template_1.ref)('ctx.stash.keys'))),
        (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.generateAuthRequestExpression = generateAuthRequestExpression;
const constructFieldMappingInput = () => {
    return (0, graphql_mapping_template_1.compoundExpression)([
        (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('lambdaInput.args.metadata.fieldMap'), (0, graphql_mapping_template_1.obj)({})),
        (0, graphql_mapping_template_1.qref)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('lambdaInput.args.metadata.fieldMap.putAll'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.defaultIfNull'), (0, graphql_mapping_template_1.ref)('context.stash.fieldMap'), (0, graphql_mapping_template_1.obj)({})))),
    ]);
};
//# sourceMappingURL=mutation.js.map