import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
/**
 * Copyright (c) 2020-present, Goldman Sachs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { useApplicationStore } from '@finos/legend-application';
import { LakehouseDataProductExecutionState, ModelAccessPointDataProductExecutionState, } from '../../stores/workflows/dataProduct/DataProductQueryBuilderState.js';
import { buildElementOption, } from '@finos/legend-lego/graph-editor';
import { CustomSelectorInput } from '@finos/legend-art';
import { QueryBuilderClassSelector } from '../QueryBuilderSideBar.js';
import { observer } from 'mobx-react-lite';
import { useEffect } from 'react';
import { flowResult } from 'mobx';
/**
 * Shared form selectors for data product query builder setup.
 * Renders the data product selector, execution context / access point group
 * selector, and class selector. Consumed by both the base setup panel and
 * the LegendQuery-specific setup panel.
 */
export const DataProductQueryBuilderSetupFormContent = observer((props) => {
    const { queryBuilderState, formatOptionLabel, isLoading } = props;
    const applicationStore = useApplicationStore();
    const onDataProductOptionChange = (option) => {
        if (option?.value) {
            queryBuilderState.handleDataProductChange(option.value);
        }
    };
    useEffect(() => {
        flowResult(queryBuilderState.loadEntities()).catch(applicationStore.alertUnhandledError);
    }, [queryBuilderState, applicationStore]);
    return (_jsxs(_Fragment, { children: [_jsxs("div", { className: "query-builder__setup__config-group__item", children: [_jsx("label", { className: "btn--sm query-builder__setup__config-group__data-product", title: "data product", htmlFor: "query-builder__setup__data-product-selector", children: "Data Product" }), _jsx(CustomSelectorInput, { inputId: "query-builder__setup__data-product-selector", className: "panel__content__form__section__dropdown query-builder__setup__config-group__item__selector", options: queryBuilderState.dataProductOptions, isLoading: isLoading ??
                            queryBuilderState.loadDataProductModelState.isInProgress, onChange: onDataProductOptionChange, value: queryBuilderState.selectedDataProductOption, placeholder: "Search for data product...", escapeClearsValue: true, darkMode: !applicationStore.layoutService
                            .TEMPORARY__isLightColorThemeEnabled, ...(formatOptionLabel ? { formatOptionLabel } : {}) })] }), Boolean(queryBuilderState.showExecutionIdSelector) && (_jsxs("div", { className: "query-builder__setup__config-group__item", children: [_jsx("label", { className: "btn--sm query-builder__setup__config-group__item__label", title: "execution id", htmlFor: "query-builder__setup__execution-id-selector", children: "Execution ID" }), _jsx(CustomSelectorInput, { inputId: "query-builder__setup__execution-id-selector", className: "panel__content__form__section__dropdown query-builder__setup__config-group__item__selector", placeholder: "Choose an execution id...", options: queryBuilderState.executionIdOptions, disabled: queryBuilderState.executionIdOptions.length < 2, onChange: (option) => {
                            queryBuilderState
                                .changeExecutionId(option)
                                .catch(applicationStore.alertUnhandledError);
                        }, value: queryBuilderState.selectedExecutionIdOption, formatOptionLabel: (option) => (_jsxs("div", { className: "query-builder__setup__config-group__item__selector__option", children: [_jsx("span", { children: option.label }), _jsx("span", { className: "query-builder__setup__config-group__item__selector__option__tag", children: option.tag })] })), darkMode: !applicationStore.layoutService
                            .TEMPORARY__isLightColorThemeEnabled })] })), !queryBuilderState.isLakehouseMode && (_jsx("div", { className: "query-builder__setup__config-group__item", children: _jsx(QueryBuilderClassSelector, { queryBuilderState: queryBuilderState, classes: queryBuilderState.usableClasses, onClassChange: queryBuilderState.onClassChange, noMatchMessage: "No compatible entity found for specified execution context" }) }))] }));
});
const DataProductQueryBuilderSetupPanelContent = observer((props) => {
    const { queryBuilderState } = props;
    const applicationStore = useApplicationStore();
    const executionState = queryBuilderState.executionState;
    // runtime options (only for model access point group)
    const runtimeRequired = executionState instanceof ModelAccessPointDataProductExecutionState ||
        executionState instanceof LakehouseDataProductExecutionState;
    const showRuntimeSelector = runtimeRequired && executionState.showRuntimeOptions;
    const runtimeOptions = runtimeRequired
        ? executionState.compatibleRuntimes.map(buildElementOption)
        : [];
    const selectedRuntimeOption = runtimeRequired && executionState.selectedRuntime
        ? buildElementOption(executionState.selectedRuntime)
        : null;
    const onRuntimeOptionChange = (option) => {
        if (runtimeRequired) {
            if (option.value === executionState.selectedRuntime) {
                return;
            }
            executionState.changeSelectedRuntime(option.value);
        }
    };
    return (_jsx("div", { className: "query-builder__setup__config-group", children: _jsxs("div", { className: "query-builder__setup__config-group__content", children: [_jsx(DataProductQueryBuilderSetupFormContent, { queryBuilderState: queryBuilderState }), Boolean(showRuntimeSelector) && (_jsxs("div", { className: "query-builder__setup__config-group__item", children: [_jsx("label", { className: "btn--sm query-builder__setup__config-group__item__label", title: "runtime", htmlFor: "query-builder__setup__runtime-selector", children: "Runtime" }), _jsx(CustomSelectorInput, { inputId: "query-builder__setup__runtime-selector", className: "panel__content__form__section__dropdown query-builder__setup__config-group__item__selector", placeholder: "Choose a runtime...", options: runtimeOptions, disabled: runtimeOptions.length < 1, onChange: onRuntimeOptionChange, value: selectedRuntimeOption, darkMode: !applicationStore.layoutService
                                .TEMPORARY__isLightColorThemeEnabled })] }))] }) }));
});
export const renderDataProductQueryBuilderSetupPanelContent = (queryBuilderState) => (_jsx(DataProductQueryBuilderSetupPanelContent, { queryBuilderState: queryBuilderState }));
//# sourceMappingURL=DataProductQueryBuilder.js.map