"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useRequestMakerStore = exports.RequestMakerProvider = exports.useRequestMaker = void 0;
const tslib_1 = require("tslib");
const lodash_1 = require("lodash");
const object_hash_1 = require("object-hash");
const React = tslib_1.__importStar(require("react"));
const request_maker_1 = require("../stores/request-maker");
const RequestMakers = new Map();
function useRequestMaker(value, checkCache = false, mockUrl) {
    let operation;
    let request;
    if (typeof value === 'object') {
        if (lodash_1.has(value, 'id') || lodash_1.has(value, 'iid') || lodash_1.has(value, 'request') || lodash_1.has(value, 'servers')) {
            operation = value;
        }
        else {
            request = value;
        }
    }
    if (checkCache) {
        return getCachedRequestMaker({ operation, request, mockUrl });
    }
    return new request_maker_1.RequestMakerStore({ operation, request, mockUrl });
}
exports.useRequestMaker = useRequestMaker;
function getCachedRequestMaker({ operation, request, mockUrl, }) {
    const id = object_hash_1.MD5({ operation, request, mockUrl });
    let requestMaker = RequestMakers.get(id);
    if (!requestMaker) {
        requestMaker = new request_maker_1.RequestMakerStore({ operation, request, mockUrl });
        RequestMakers.set(id, requestMaker);
    }
    return requestMaker;
}
const RequestMakerStoreContext = React.createContext(new request_maker_1.RequestMakerStore());
exports.RequestMakerProvider = RequestMakerStoreContext.Provider;
function useRequestMakerStore(name) {
    const requestMaker = React.useContext(RequestMakerStoreContext);
    if (name) {
        return requestMaker[name];
    }
    else {
        return requestMaker;
    }
}
exports.useRequestMakerStore = useRequestMakerStore;
//# sourceMappingURL=useRequestMaker.js.map