import { jsx as _jsx } from "react/jsx-runtime";
import { flowResult } from 'mobx';
import { AccessorQueryBuilderState, QueryBuilderActionConfig, QueryBuilderAdvancedWorkflowState, getCompatibleRuntimesFromAccessorOwner, } from '@finos/legend-query-builder';
import { assertErrorThrown, guaranteeNonNullable } from '@finos/legend-shared';
import { PromoteAccessorQueryToFunctionAction } from './AccessorQueryBuilder.js';
export const queryAccessorSource = async (element, editorStore) => {
    try {
        const embeddedQueryBuilderState = editorStore.embeddedQueryBuilderState;
        await flowResult(embeddedQueryBuilderState.setEmbeddedQueryBuilderConfiguration({
            setupQueryBuilderState: async () => {
                const queryBuilderState = new AccessorQueryBuilderState(editorStore.applicationStore, undefined, editorStore.graphManagerState, QueryBuilderAdvancedWorkflowState.INSTANCE, QueryBuilderActionConfig.INSTANCE, editorStore.applicationStore.config.options.queryBuilderConfig, editorStore.editorMode.getSourceInfo());
                await queryBuilderState.changeAccessorOwner(element);
                const compatibleRuntimes = getCompatibleRuntimesFromAccessorOwner(element, editorStore.graphManagerState);
                if (compatibleRuntimes.length > 0) {
                    queryBuilderState.changeSelectedRuntime(guaranteeNonNullable(compatibleRuntimes[0]));
                }
                return queryBuilderState;
            },
            disableCompile: true,
            actionConfigs: [
                {
                    key: 'promote-accessor-query-to-function-btn',
                    renderer: (queryBuilderState) => (_jsx(PromoteAccessorQueryToFunctionAction, { queryBuilderState: queryBuilderState })),
                },
            ],
        }));
    }
    catch (error) {
        assertErrorThrown(error);
        editorStore.applicationStore.notificationService.notifyError(`Failed to open query builder: ${error.message}`);
    }
};
//# sourceMappingURL=AccessorQueryBuilderHelper.js.map