import { jsx as _jsx } from "react/jsx-runtime";
import { Alert, Block } from '@cube-dev/ui-kit';
import sqlFormatter from 'sql-formatter';
import { CubeSQLConverter } from './utils/cube-sql-converter';
import { useDeepMemo } from './hooks';
import { useQueryBuilderContext } from './context';
import { CopyButton } from './components/CopyButton';
import { TabPaneWithToolbar } from './components/TabPaneWithToolbar';
import { ScrollableCodeContainer } from './components/ScrollableCodeContainer';
export function QueryBuilderSQL() {
    const { queryHash, meta, isQueryEmpty, verificationError, dryRunResponse } = useQueryBuilderContext();
    return useDeepMemo(() => {
        // todo: fix types of normalizedQueries (e.g. order is always an array)
        const [query] = dryRunResponse?.normalizedQueries || [];
        if (isQueryEmpty) {
            return (_jsx(Block, { padding: "1x", children: _jsx(Alert, { children: "Compose a query to see an SQL query." }) }));
        }
        if (!query) {
            return (_jsx(Block, { padding: "1x", children: _jsx(Alert, { children: "Unable to generate an SQL query." }) }));
        }
        if (!isQueryEmpty && meta) {
            if (verificationError) {
                return (_jsx(Block, { padding: "1x", children: _jsx(Alert, { theme: "danger", children: verificationError.toString() }) }));
            }
            let sqlQuery = '';
            try {
                // @ts-ignore
                const converter = new CubeSQLConverter(query, meta.meta);
                sqlQuery = sqlFormatter.format(converter.buildQuery());
            }
            catch (e) {
                return (_jsx(Block, { padding: "1x", children: _jsx(Alert, { theme: "danger", children: e.message }) }));
            }
            return (_jsx(TabPaneWithToolbar, { actions: _jsx(CopyButton, { type: "secondary", value: sqlQuery, children: "Copy" }), children: _jsx(ScrollableCodeContainer, { value: sqlQuery }) }));
        }
        else {
            return (_jsx(Block, { padding: "1x", children: _jsx(Alert, { theme: "note", children: "Compose a query to see an SQL query." }) }));
        }
    }, [dryRunResponse, queryHash, verificationError, meta]);
}
//# sourceMappingURL=QueryBuilderSQL.js.map