import { jsx as _jsx, jsxs as _jsxs } 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 { buildExecOptions, buildModelAccessPointGroupOption, NativeModelDataProductExecutionState, 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 { resolveUsableDataProductClasses, } from '@finos/legend-graph';
import { useEffect } from 'react';
import { flowResult } from 'mobx';
export const buildNativeExecutionContextOption = (value) => ({
    label: value.key,
    value,
});
export const buildDataProductRuntimeInfoOption = (value) => ({
    label: value.key,
    value,
});
export const buildDataProductOption = (value) => ({
    label: value.name,
    value,
});
const DataProductQueryBuilderSetupPanelContent = observer((props) => {
    const { queryBuilderState } = props;
    const applicationStore = useApplicationStore();
    const executionState = queryBuilderState.executionState;
    // data product
    const dataProductOptions = queryBuilderState.dataProductOptions;
    const selectedDataProductOption = queryBuilderState.selectedDataProductOption;
    const onDataProductOptionChange = (option) => {
        if (option?.value) {
            queryBuilderState.handleDataProductChange(option.value);
        }
    };
    const isNativeMode = executionState instanceof NativeModelDataProductExecutionState;
    const isModelAccessPointGroupMode = executionState instanceof ModelAccessPointDataProductExecutionState;
    // native execution context options
    const executionContextOptions = queryBuilderState.execOptions;
    const showExecutionContextOptions = isNativeMode && executionContextOptions.length > 1;
    const selectedExecOption = executionState instanceof NativeModelDataProductExecutionState
        ? buildExecOptions(executionState.exectionValue)
        : undefined;
    // model access point group options
    const modelAccessPointGroupOptions = queryBuilderState.modelAccessPointGroupOptions;
    const showModelAccessPointGroupSelector = isModelAccessPointGroupMode && modelAccessPointGroupOptions.length > 1;
    const selectedModelAccessPointGroupOption = executionState instanceof ModelAccessPointDataProductExecutionState
        ? buildModelAccessPointGroupOption(executionState.exectionValue)
        : undefined;
    // runtime options (only for model access point group)
    const showRuntimeSelector = executionState instanceof ModelAccessPointDataProductExecutionState &&
        executionState.showRuntimeOptions;
    const runtimeOptions = executionState instanceof ModelAccessPointDataProductExecutionState
        ? executionState.compatibleRuntimes.map(buildElementOption)
        : [];
    const selectedRuntimeOption = executionState instanceof ModelAccessPointDataProductExecutionState &&
        executionState.selectedRuntime
        ? buildElementOption(executionState.selectedRuntime)
        : null;
    const onRuntimeOptionChange = (option) => {
        if (executionState instanceof ModelAccessPointDataProductExecutionState &&
            option.value === executionState.selectedRuntime) {
            return;
        }
        if (executionState instanceof ModelAccessPointDataProductExecutionState) {
            executionState.changeSelectedRuntime(option.value);
        }
    };
    // class — resolve from the active mapping and featured elements
    const activeMapping = queryBuilderState.activeMapping;
    const activeFeaturedElements = queryBuilderState.activeFeaturedElements;
    const classes = activeMapping
        ? resolveUsableDataProductClasses(activeFeaturedElements, activeMapping, queryBuilderState.graphManagerState, undefined)
        : [];
    useEffect(() => {
        flowResult(queryBuilderState.loadEntities()).catch(applicationStore.alertUnhandledError);
    }, [queryBuilderState, applicationStore]);
    const onExecutionContextOptionChange = async (option) => {
        if (isNativeMode && option.value === executionState.exectionValue) {
            return;
        }
        queryBuilderState.setExecutionState(option.value);
        await queryBuilderState.propagateExecutionContextChange();
        queryBuilderState.onExecutionContextChange?.(option.value);
    };
    const handleExecutionContextOptionChange = (option) => {
        flowResult(onExecutionContextOptionChange(option));
    };
    const onModelAccessPointGroupOptionChange = async (option) => {
        if (isModelAccessPointGroupMode &&
            option.value === executionState.exectionValue) {
            return;
        }
        queryBuilderState.setExecutionState(option.value);
        await queryBuilderState.propagateExecutionContextChange();
    };
    const handleModelAccessPointGroupOptionChange = (option) => {
        flowResult(onModelAccessPointGroupOptionChange(option));
    };
    return (_jsx("div", { className: "query-builder__setup__config-group", children: _jsxs("div", { className: "query-builder__setup__config-group__content", 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: dataProductOptions, isLoading: queryBuilderState.loadDataProductModelState.isInProgress, onChange: onDataProductOptionChange, value: selectedDataProductOption, placeholder: "Search for data product...", escapeClearsValue: true, darkMode: !applicationStore.layoutService
                                .TEMPORARY__isLightColorThemeEnabled })] }), Boolean(showExecutionContextOptions) && (_jsxs("div", { className: "query-builder__setup__config-group__item", children: [_jsx("label", { className: "btn--sm query-builder__setup__config-group__item__label", title: "execution context", htmlFor: "query-builder__setup__context-selector", children: "Context" }), _jsx(CustomSelectorInput, { inputId: "query-builder__setup__context-selector", className: "panel__content__form__section__dropdown query-builder__setup__config-group__item__selector", placeholder: "Choose an execution context...", options: executionContextOptions, disabled: executionContextOptions.length < 1 ||
                                (executionContextOptions.length === 1 &&
                                    Boolean(selectedExecOption)), onChange: handleExecutionContextOptionChange, value: selectedExecOption, darkMode: !applicationStore.layoutService
                                .TEMPORARY__isLightColorThemeEnabled })] })), Boolean(showModelAccessPointGroupSelector) && (_jsxs("div", { className: "query-builder__setup__config-group__item", children: [_jsx("label", { className: "btn--sm query-builder__setup__config-group__item__label", title: "access point group", htmlFor: "query-builder__setup__access-point-group-selector", children: "Access Point Group" }), _jsx(CustomSelectorInput, { inputId: "query-builder__setup__access-point-group-selector", className: "panel__content__form__section__dropdown query-builder__setup__config-group__item__selector", placeholder: "Choose an access point group...", options: modelAccessPointGroupOptions, disabled: modelAccessPointGroupOptions.length < 1, onChange: handleModelAccessPointGroupOptionChange, value: selectedModelAccessPointGroupOption, darkMode: !applicationStore.layoutService
                                .TEMPORARY__isLightColorThemeEnabled })] })), 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 })] })), _jsx("div", { className: "query-builder__setup__config-group__item", children: _jsx(QueryBuilderClassSelector, { queryBuilderState: queryBuilderState, classes: classes, onClassChange: queryBuilderState.onClassChange, noMatchMessage: "No compatible entity found for specified execution context" }) })] }) }));
});
export const renderDataProductQueryBuilderSetupPanelContent = (queryBuilderState) => (_jsx(DataProductQueryBuilderSetupPanelContent, { queryBuilderState: queryBuilderState }));
//# sourceMappingURL=DataProductQueryBuilder.js.map