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 { CustomSelectorInput, PanelHeader, compareLabelFn, createFilter, } from '@finos/legend-art';
import { observer } from 'mobx-react-lite';
import { RuntimePointer, PackageableElementExplicitReference, } from '@finos/legend-graph';
import { useApplicationStore } from '@finos/legend-application';
import { buildRuntimeValueOption, getRuntimeOptionFormatter, } from '../QueryBuilderSideBar.js';
import { getPackageableElementOptionFormatter } from '@finos/legend-lego/graph-editor';
const AccessorQueryBuilderSetupPanelContent = observer((props) => {
    const { queryBuilderState } = props;
    const applicationStore = useApplicationStore();
    // accessor owner (IngestDefinition or Database)
    const accessorOwnerOptions = queryBuilderState.accessorOwnerOptions.sort(compareLabelFn);
    const selectedAccessorOwnerOption = queryBuilderState.selectedAccessorOwner
        ? (queryBuilderState.accessorOwnerOptions.find((opt) => opt.value === queryBuilderState.selectedAccessorOwner) ?? null)
        : null;
    const changeAccessorOwner = async (val) => {
        if (val.value === queryBuilderState.selectedAccessorOwner) {
            return;
        }
        await queryBuilderState.changeAccessorOwner(val.value);
    };
    const accessorOwnerFilterOption = createFilter({
        ignoreCase: true,
        ignoreAccents: false,
        stringify: (option) => option.data.value.path,
    });
    // accessor (dataset / table)
    const accessorOptions = queryBuilderState.accessorsOptions.sort(compareLabelFn);
    const selectedAccessorOption = queryBuilderState.sourceAccessor
        ? (queryBuilderState.accessorsOptions.find((opt) => opt.value.tableName ===
            queryBuilderState.sourceAccessor?.accessor &&
            opt.value.schemaName === queryBuilderState.sourceAccessor.schema) ?? null)
        : null;
    const changeAccessor = async (val) => {
        await queryBuilderState.changeAccessor(val.value);
    };
    const showAccessorSelector = accessorOptions.length > 0;
    // runtime
    const runtimeOptions = queryBuilderState.compatibleRuntimes
        .map((rt) => new RuntimePointer(PackageableElementExplicitReference.create(rt)))
        .map(buildRuntimeValueOption)
        .sort(compareLabelFn);
    const selectedRuntimeOption = queryBuilderState.executionContextState
        .runtimeValue
        ? buildRuntimeValueOption(queryBuilderState.executionContextState.runtimeValue)
        : null;
    const changeRuntime = (val) => {
        if (val.value === queryBuilderState.executionContextState.runtimeValue) {
            return;
        }
        queryBuilderState.changeRuntime(val.value);
    };
    const runtimeFilterOption = createFilter({
        ignoreCase: true,
        ignoreAccents: false,
        stringify: (option) => option.data.value instanceof RuntimePointer
            ? option.data.value.packageableRuntime.value.path
            : 'custom',
    });
    // Hide the runtime selector entirely when the builder reports no
    // compatible runtimes (e.g. ingest deep-link flow always uses an adhoc
    // runtime selected upstream).
    const showRuntimeSelector = queryBuilderState.selectedAccessorOwner === undefined ||
        runtimeOptions.length > 0;
    return (_jsxs("div", { className: "query-builder__setup__config-group", children: [_jsx(PanelHeader, { title: "properties" }), _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__item__label", title: "source", htmlFor: "query-builder__setup__source-selector", children: "Source" }), _jsx(CustomSelectorInput, { inputId: "query-builder__setup__source-selector", className: "panel__content__form__section__dropdown query-builder__setup__config-group__item__selector", placeholder: "Choose a source...", options: accessorOwnerOptions, onChange: (val) => {
                                    changeAccessorOwner(val).catch(() => undefined);
                                }, value: selectedAccessorOwnerOption, darkMode: !applicationStore.layoutService
                                    .TEMPORARY__isLightColorThemeEnabled, filterOption: accessorOwnerFilterOption, formatOptionLabel: getPackageableElementOptionFormatter({
                                    darkMode: !applicationStore.layoutService
                                        .TEMPORARY__isLightColorThemeEnabled,
                                }) })] }), showAccessorSelector && (_jsxs("div", { className: "query-builder__setup__config-group__item", children: [_jsx("label", { className: "btn--sm query-builder__setup__config-group__item__label", title: "accessor", htmlFor: "query-builder__setup__accessor-selector", children: queryBuilderState.accessorLabel }), _jsx(CustomSelectorInput, { inputId: "query-builder__setup__accessor-selector", className: "panel__content__form__section__dropdown query-builder__setup__config-group__item__selector", placeholder: `Choose a ${queryBuilderState.accessorLabel.toLocaleLowerCase()}...`, options: accessorOptions, onChange: (val) => {
                                    changeAccessor(val).catch(() => undefined);
                                }, value: selectedAccessorOption, darkMode: !applicationStore.layoutService
                                    .TEMPORARY__isLightColorThemeEnabled })] })), 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...", noMatchMessage: "No compatible runtime found", disabled: !queryBuilderState.selectedAccessorOwner, options: runtimeOptions, onChange: changeRuntime, value: selectedRuntimeOption, darkMode: !applicationStore.layoutService
                                    .TEMPORARY__isLightColorThemeEnabled, filterOption: runtimeFilterOption, formatOptionLabel: getRuntimeOptionFormatter({
                                    darkMode: !applicationStore.layoutService
                                        .TEMPORARY__isLightColorThemeEnabled,
                                }) })] }))] })] }));
});
export const renderAccessorQueryBuilderSetupPanelContent = (queryBuilderState) => (_jsx(AccessorQueryBuilderSetupPanelContent, { queryBuilderState: queryBuilderState }));
//# sourceMappingURL=AccessorQueryBuilder.js.map