"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const datalayer_integration_1 = require("./datalayer-integration");
const React = __importStar(require("react"));
// create empty context as default
const DataLayerContext = React.createContext({});
/**
 * This HOC attaches the req sent to the server down to the Components - on server side only, of course!
 *
 * see hocs with context: https://itnext.io/combining-hocs-with-the-new-reacts-context-api-9d3617dccf0b
 *
 * When using the req: either check whether it is undefined or whether we run on the server -->
 * how to check whether running on server or in browser: https://www.npmjs.com/package/exenv
 */
const AttachDataLayer = (props) => {
    //console.log("AttachDataLayer: ", props.dataLayer);
    return React.createElement(DataLayerContext.Provider, { value: {
            apolloClient: props.apolloClient,
            dataLayer: props.dataLayer
        } }, props.children);
};
/**
 * @param Component
 * @returns {function(any): any}
 */
function withDataLayer(Component) {
    return function WrapperComponent(props) {
        return (React.createElement(DataLayerContext.Consumer, null, (context) => {
            //console.log("value of context: ", context);
            const entryListQuery = (entryId, dictKey) => {
                //console.log("attach datalayer layer: ", context.dataLayer["isOffline"])
                return context.dataLayer.getEntryListQuery(entryId, dictKey);
            };
            const getEntryQuery = (entryId, dictKey) => {
                return context.dataLayer.getEntryQuery(entryId, dictKey);
            };
            const setEntryMutation = (entryId, values) => {
                //console.log("attach datalayer layer: ", context.dataLayer["isOffline"])
                return context.dataLayer.setEntryMutation(entryId, values);
            };
            const getEntryScanQuery = (entryId, dictKey) => {
                return context.dataLayer.getEntryScanQuery(entryId, dictKey);
            };
            const deleteEntryMutation = (entryId, values) => {
                return context.dataLayer.deleteEntryMutation(entryId, values);
            };
            const createSetMutation = (useMutation, entryId, values) => {
                const { mutation } = setEntryMutation(entryId, values);
                const [f, { data }] = useMutation(mutation, {
                    context: context,
                    client: context.apolloClient
                });
                return f;
            };
            const createDeleteMutation = (useMutation, entryId, values) => {
                const { mutation } = deleteEntryMutation(entryId, values);
                const [f, { data }] = useMutation(mutation, {
                    context: context,
                    client: context.apolloClient
                });
                return f;
            };
            const createQuery = (useQuery, { query }) => {
                return useQuery(query, {
                    context: context,
                    client: context.apolloClient
                });
            };
            const updateEntryQuery = (entryId, fDictKey) => {
                return context.dataLayer.updateEntryQuery(entryId, fDictKey);
            };
            //console.log("entryListQuery: ", entryListQuery);
            return React.createElement(Component, Object.assign({}, props, { apolloClient: context.apolloClient, getEntryListQuery: entryListQuery, getEntryQuery: getEntryQuery, setEntryMutation: setEntryMutation, getEntryScanQuery: getEntryScanQuery, deleteEntryMutation: deleteEntryMutation, createSetMutation: createSetMutation, createDeleteMutation: createDeleteMutation, createQuery: createQuery, updateEntryQuery: updateEntryQuery }));
        }));
    };
}
exports.withDataLayer = withDataLayer;
exports.serviceWithDataLayer = (complementedCallback) => {
    // we return an array of valid middleware-callbacks
    return [
        function (req, res, next) {
            return __awaiter(this, void 0, void 0, function* () {
                return yield complementedCallback(req.dataLayer, req, res, next);
            });
        }
    ];
};
exports.serviceAttachDataLayer = (dataLayer) => {
    return (req, res, next) => {
        const client = datalayer_integration_1.createApolloClient(dataLayer, datalayer_integration_1.getGraphqlUrl(dataLayer.isOffline), req);
        dataLayer.setClient(client);
        //console.log("attaching the dataLayer, client: ", client);
        req.dataLayer = dataLayer;
        req.dataLayer.client = client;
        next();
    };
};
exports.default = AttachDataLayer;
//# sourceMappingURL=attach-data-layer.js.map