import React, { useEffect } from "react";
import { MS_IN_SECOND } from "../../../utils/utils";
import { serverDefault } from "../../../utils/server-detection";
import { selectIsSnapshotMode, selectSnapshotOptions } from "../../dashboard/selectors";
import { selectGlobalPanAndZoom } from "../../global/selectors";
import { useDispatch, useSelector } from "../../../store/redux-separate-context";
import { fetchDataForSnapshotAction } from "../actions";
import { chartLibrariesSettings } from "../utils/chartLibrariesSettings";
import { getChartURLOptions } from "../utils/get-chart-url-options";
const SnapshotLoader = ({ attributes, chartUuid, }) => {
    const host = attributes.host || serverDefault;
    const { snapshotDataPoints } = useSelector(selectSnapshotOptions);
    const group = attributes.method || window.NETDATA.chartDefaults.method;
    const { chartLibrary } = attributes;
    const chartSettings = chartLibrariesSettings[chartLibrary];
    const globalPanAndZoom = useSelector(selectGlobalPanAndZoom);
    const after = globalPanAndZoom.after / MS_IN_SECOND;
    const before = globalPanAndZoom.before / MS_IN_SECOND;
    const dispatch = useDispatch();
    useEffect(() => {
        dispatch(fetchDataForSnapshotAction.request({
            // properties to be passed to API
            host,
            context: attributes.id,
            chart: attributes.id,
            format: chartSettings.format,
            points: snapshotDataPoints,
            group,
            gtime: attributes.gtime || 0,
            // for snapshots, always eliminate zero dimensions
            options: getChartURLOptions(attributes, true),
            after: after || null,
            before: before || null,
            dimensions: attributes.dimensions,
            aggrMethod: attributes.aggrMethod,
            nodeIDs: attributes.nodeIDs,
            chartLibrary,
            id: chartUuid,
            groupBy: attributes.groupBy,
        }));
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }); // todo fetch based on state
    return null;
};
export const SnapshotLoaderContainer = ({ attributes, chartUuid, }) => {
    const isSnapshotMode = useSelector(selectIsSnapshotMode);
    if (!isSnapshotMode) {
        return null;
    }
    return React.createElement(SnapshotLoader, { attributes: attributes, chartUuid: chartUuid });
};
//# sourceMappingURL=snapshot-loader.js.map