"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useResults = void 0;
const react_1 = __importDefault(require("react"));
const utility_1 = require("../utility");
/** This hook must never be used to render results */
const useResults = () => {
    const [results, setResults] = react_1.default.useState([]);
    const [resultStats, setResultStats] = react_1.default.useState();
    const loading = react_1.default.useMemo(() => {
        return typeof (resultStats === null || resultStats === void 0 ? void 0 : resultStats.numberOfResults) === "undefined";
    }, [resultStats === null || resultStats === void 0 ? void 0 : resultStats.numberOfResults]);
    react_1.default.useEffect(() => {
        const handler = (event) => {
            setResults(event.detail);
        };
        window.addEventListener(`@usereactify/search:results:update`, handler);
        return () => window.removeEventListener(`@usereactify/search:results:update`, handler);
    }, []);
    react_1.default.useEffect(() => {
        const handler = (event) => {
            setResultStats(event.detail);
        };
        window.addEventListener(`@usereactify/search:resultStats:update`, handler);
        return () => window.removeEventListener(`@usereactify/search:resultStats:update`, handler);
    }, []);
    const handleSetResults = (value) => {
        const event = new CustomEvent(`@usereactify/search:results:update`, {
            detail: value
        });
        window.dispatchEvent(event);
    };
    const handleSetResultStats = (value) => {
        const event = new CustomEvent(`@usereactify/search:resultStats:update`, {
            detail: value
        });
        window.dispatchEvent(event);
    };
    utility_1.debug.hook("useResults", "results", results);
    utility_1.debug.hook("useResults", "resultStats", resultStats);
    return {
        loading: loading,
        results: results,
        setResults: handleSetResults,
        resultStats: resultStats,
        setResultStats: handleSetResultStats,
    };
};
exports.useResults = useResults;
//# sourceMappingURL=useResults.js.map