"use strict";
/**
 * Created by frank.zickert on 02.05.19.
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const aws_sdk_1 = __importDefault(require("aws-sdk"));
const graphql_tag_1 = __importDefault(require("graphql-tag"));
const universal_cookie_1 = __importDefault(require("universal-cookie"));
/**
 * transforms a function into a Promise
 */
const promisify = foo => new Promise((resolve, reject) => {
    foo((error, result) => {
        if (error) {
            reject(error);
        }
        else {
            resolve(result);
        }
    });
});
exports.setEntry = (tableName, pkEntity, pkId, skEntity, skId, jsonData) => {
    console.log("setEntry: ", pkEntity, "|", pkId, "|", skEntity, "|", skId);
    return promisify(callback => new aws_sdk_1.default.DynamoDB.DocumentClient().update({
        TableName: tableName,
        /**
         * ALL KEYS MUST BE SPECIFIED HERE!
         */
        Key: {
            pk: `${pkEntity}|${pkId}`,
            sk: `${skEntity}|${skId}`
        },
        UpdateExpression: `SET jsonData = :jsonData`,
        ExpressionAttributeValues: {
            ':jsonData': `${JSON.stringify(jsonData)}`,
        }
    }, callback))
        .then(() => {
        console.log("data stored!");
        var result = {};
        result[pkEntity] = pkId;
        result[skEntity] = skId;
        result["data"] = `${JSON.stringify(jsonData).replace(/"/g, "\\\"")}`;
        return result;
    }).catch(error => { console.log(error); });
};
/**
 * Get all entries to a entity|value pair in the key-field whose range have the specified rangeEntity
 *
 * @param key specify which field is the key: pk or sk
 * @param entity specifies the entity of the key-field
 * @param value specify the id of the key-field
 * @param rangeEntity specify the entity of the range
 * @returns {Promise<string>|any}
 */
exports.ddbListEntries = (tableName, key, entity, value, rangeEntity) => {
    console.log("ddbListEntries: ", tableName, key, entity, value, rangeEntity);
    const q = {
        // use the table_name as specified in the serverless.yml
        TableName: tableName,
        IndexName: key === "sk" ? "reverse" : undefined,
        /**
         * ALL KEYS MUST HAVE KEY-CONDITION-EXPRESSIONS!
         */
        KeyConditionExpression: `${key} = :value and begins_with(${key === "pk" ? "sk" : "pk"}, :entity)`,
        ExpressionAttributeValues: {
            ":value": `${entity}|${value}`,
            ":entity": rangeEntity
        }
    };
    console.log("query: ", q);
    return promisify(callback => new aws_sdk_1.default.DynamoDB.DocumentClient().query(q, callback))
        .then(result => {
        console.log("ddb-result: ", result);
        return result["Items"];
        /*
         if (result.Items) {

         return result.Items.map(item => JSON.stringify(item));
         }

         return [];*/
        //return result.Items.map(item => JSON.stringify(item));
    }).catch(error => { console.log(error); });
};
exports.getEntry = (tableName, pkEntity, pkValue, skEntity, skValue) => {
    console.log("pk: ", `${pkEntity}|${pkValue}`);
    console.log("sk: ", `${skEntity}|${skValue}`);
    return promisify(callback => new aws_sdk_1.default.DynamoDB.DocumentClient().get({
        // use the table_name as specified in the serverless.yml
        TableName: tableName,
        Key: {
            pk: `${pkEntity}|${pkValue}`,
            sk: `${skEntity}|${skValue}`
        }
    }, callback))
        .then(result => {
        console.log("result: ", result);
        return result["Item"] ? result["Item"] : result;
    }).catch(error => { console.log(error); });
};
const typed_graphqlify_1 = require("typed-graphqlify");
const auth_middleware_1 = require("../authentication/auth-middleware");
/**
 * this function provides a executable graphql-query
 * TODO the fields must be taken from the data-layer, not requiring the user to provide them
 */
exports.setEntryMutation = (entryId, data, fields, context = {}) => {
    console.log("setEntryMutation: ", entryId, data, fields);
    const mutationObj = {};
    mutationObj[`set_${entryId}`] = typed_graphqlify_1.params(Object.keys(data).reduce((result, key) => {
        result[key] = `"${data[key]}"`;
        return result;
    }, {}), Object.keys(fields).reduce((result, key) => {
        result[key] = typed_graphqlify_1.types.string;
        return result;
    }, {}));
    return {
        mutation: graphql_tag_1.default `${typed_graphqlify_1.mutation(mutationObj)}`,
        context: context
    };
};
/**
 * this function provides a executable graphql-query
 * TODO the fields must be taken from the data-layer, not requiring the user to provide them
 */
exports.getEntryListQuery = (entryId, data, fields, context = {}) => {
    console.log("getEntryListQuery: ", entryId, data, fields, context);
    if (Object.keys(data).length !== 1) {
        console.error("getEntryListQuery requires exact 1 field provided in the data argument");
        return undefined;
    }
    const queryKey = Object.keys(data)[0];
    const queryObj = {};
    queryObj[`list_${entryId}_${queryKey}`] = typed_graphqlify_1.params(Object.keys(data).filter(key => key === queryKey).reduce((result, key) => {
        result[key] = `"${data[key]}"`;
        return result;
    }, {}), Object.keys(fields).reduce((result, key) => {
        result[key] = typed_graphqlify_1.types.string;
        return result;
    }, {}));
    console.log("listQuery string: ", typed_graphqlify_1.query(queryObj));
    return {
        query: graphql_tag_1.default `${typed_graphqlify_1.query(queryObj)}`,
        context: context
    };
};
exports.select = (client, { query, context = {} }) => {
    if (!context["userId"]) {
        context["userId"] = new universal_cookie_1.default().get(auth_middleware_1.IC_USER_ID);
    }
    console.log("select: ", query, context);
    return client.query({
        query: query,
        context: context
    }).then(result => {
        console.log("select result: ", result);
        return result.data;
    }).catch(error => {
        console.log(error);
    });
};
/**
 * uses this: https://github.com/acro5piano/typed-graphqlify
 *
 * TODO generalize to other data-types than string
 *
 * @param client
 * @param entryId
 * @param data
 * @returns {any|Promise<T>|Promise<U>}
 */
exports.mutate = (client, { mutation, context = {} }) => {
    if (!context["userId"]) {
        context["userId"] = new universal_cookie_1.default().get(auth_middleware_1.IC_USER_ID);
    }
    console.log("mutate: ", mutation, context);
    //console.log("mutation string: ", mutation(mutationObj));
    return client.mutate({
        mutation: mutation,
        context: context
    }).then(result => { console.log(result); }).catch(error => { console.log(error); });
};
//# sourceMappingURL=datalayer-libs.js.map