"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createContentTypeSuggestion = exports.useSuggestion = void 0;
const tslib_1 = require("tslib");
const mobx_1 = require("mobx");
const React = tslib_1.__importStar(require("react"));
exports.useSuggestion = (possibleSuggestions, input) => {
    const [currentSuggestion, setCurrentSuggestion] = React.useState();
    React.useEffect(() => {
        return mobx_1.autorun(() => {
            for (const suggestion of possibleSuggestions) {
                const evaluationResult = suggestion(input);
                if (evaluationResult) {
                    setCurrentSuggestion(evaluationResult);
                    return;
                }
            }
            setCurrentSuggestion(undefined);
            return;
        }, { delay: 300 });
    }, [input, possibleSuggestions]);
    return currentSuggestion;
};
exports.createContentTypeSuggestion = (condition, contentTypeHeaderValue, render) => {
    return (store) => {
        if (!condition(store))
            return;
        const requestStore = store.request;
        const resolution = () => {
            for (const [index, header] of requestStore.headerParams.entries()) {
                if (header.name.toLowerCase() === 'content-type') {
                    requestStore.setParam('header', index, 'isEnabled', false);
                }
            }
            const correctHeader = requestStore.headerParams.findIndex(p => p.name.toLowerCase() === 'content-type' && p.value === contentTypeHeaderValue);
            if (correctHeader > -1) {
                requestStore.setParam('header', correctHeader, 'isEnabled', true);
            }
            else {
                requestStore.addParam('header', 'Content-Type', contentTypeHeaderValue);
            }
        };
        return render(resolution);
    };
};
//# sourceMappingURL=useSuggestion.js.map