"use strict";
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 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) => {
                return context.dataLayer.getEntryListQuery(entryId, dictKey);
                /*
                const fields = context.dataLayer.getEntryDataFields(entryId);
                //console.log("fields: ", fields);

                return getEntryListQuery(
                    entryId,
                    dictKey,
                    fields
                );*/
            };
            const setEntryMutation = (entryId, values) => {
                return context.dataLayer.setEntryMutation(entryId, values);
            };
            //console.log("entryListQuery: ", entryListQuery);
            return React.createElement(Component, Object.assign({}, props, { apolloClient: context.apolloClient, getEntryListQuery: entryListQuery, setEntryMutation: setEntryMutation }));
        }));
    };
}
exports.withDataLayer = withDataLayer;
exports.default = AttachDataLayer;
//# sourceMappingURL=attach-data-layer.js.map