import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Alert, Block, Card, PrismCode, Title } from '@cube-dev/ui-kit';
import cube from '@cubejs-client/core';
import { useEffect, useMemo } from 'react';
import { QueryBuilderContext } from './context';
import { useLocalStorage } from './hooks';
import { useQueryBuilder } from './hooks/query-builder';
import { QueryBuilderInternals } from './QueryBuilderInternals';
import { useCommitPress } from './utils/use-commit-press';
export function QueryBuilder(props) {
    const { apiUrl, apiToken, defaultChartType, defaultPivotConfig, onQueryChange, defaultQuery, shouldRunDefaultQuery, schemaVersion, tracking, isApiBlocked, apiVersion, memberViewType, VizardComponent, RequestStatusComponent, openSqlRunner, displayPrivateItems, disableSidebarResizing, } = props;
    const cubeApi = useMemo(() => {
        return apiUrl && apiToken && apiToken !== 'undefined'
            ? cube(apiToken, {
                apiUrl,
            })
            : undefined;
    }, [apiUrl, apiToken]);
    const [storedTimezones] = useLocalStorage('QueryBuilder:timezones', []);
    function queryValidator(query) {
        const queryCopy = JSON.parse(JSON.stringify(query));
        // add the last stored timezone if the query is empty
        if (JSON.stringify(queryCopy) === '{}' && storedTimezones[0]) {
            queryCopy.timezone = storedTimezones[0];
        }
        return queryCopy;
    }
    const { runQuery, cubes, isCubeJoined, usedCubes, getCubeByName, meta, loadMeta, metaError, richMetaError, selectCube, selectedCube, ...otherProps } = useQueryBuilder({
        cubeApi,
        defaultQuery,
        defaultChartType,
        defaultPivotConfig,
        schemaVersion,
        onQueryChange,
        memberViewType,
        tracking,
        queryValidator,
        displayPrivateItems,
    });
    useEffect(() => {
        if (defaultQuery && shouldRunDefaultQuery && meta) {
            void runQuery();
        }
    }, [shouldRunDefaultQuery, meta]);
    useCommitPress(() => {
        return runQuery();
    }, true);
    if (!apiToken || !cubeApi || !apiUrl) {
        return null;
    }
    return (_jsx(QueryBuilderContext.Provider, { value: {
            runQuery,
            cubes,
            isCubeJoined,
            meta,
            loadMeta,
            metaError,
            richMetaError,
            selectedCube,
            selectCube,
            usedCubes,
            getCubeByName,
            tracking,
            isApiBlocked,
            apiToken,
            apiUrl,
            apiVersion,
            VizardComponent,
            RequestStatusComponent,
            openSqlRunner,
            disableSidebarResizing,
            ...otherProps,
        }, children: !meta ? (_jsx(Block, { flexGrow: 1, padding: "2x", children: !metaError ? (_jsx(Card, { children: "Loading meta information..." })) : (_jsxs(Alert, { theme: "danger", children: [_jsx(Title, { level: 5, children: "Unable to load meta data." }), _jsx(PrismCode, { code: metaError })] })) })) : props.children ? (props.children) : (_jsx(QueryBuilderInternals, {})) }));
}
//# sourceMappingURL=QueryBuilder.js.map