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 { useState, useEffect, useRef } from 'react';
import { useDrag } from 'react-dnd';
import { clsx, TreeView, PURE_PrimitiveTypeIcon, } from '@finos/legend-art';
import { CORE_DND_TYPE, FlatDataColumnDragSource, } from '../../../../stores/editor/utils/DnDUtils.js';
import { addUniqueEntry } from '@finos/legend-shared';
import { DEFAULT_SOURCE_PARAMETER_NAME, } from '@finos/legend-graph';
const getRecordTypeTreeNodeData = (field, parentRecordType) => ({
    id: `${DEFAULT_SOURCE_PARAMETER_NAME}[${field.address ? field.address : `'${field.label}'`}]`,
    label: field.label,
    field: field,
    parentType: parentRecordType,
    isOpen: true,
});
const getRecordTypeTreeData = (recordType) => {
    const rootIds = [];
    const nodes = new Map();
    recordType.fields
        .toSorted((a, b) => a.label.toString().localeCompare(b.label.toString()))
        .forEach((field) => {
        const recordTypeTreeNodeData = getRecordTypeTreeNodeData(field, recordType);
        addUniqueEntry(rootIds, recordTypeTreeNodeData.id);
        nodes.set(recordTypeTreeNodeData.id, recordTypeTreeNodeData);
    });
    return { rootIds, nodes };
};
const RecordFieldTreeNodeContainer = (props) => {
    const { node, level, stepPaddingInRem, onNodeSelect, innerProps } = props;
    const { selectedType } = innerProps;
    const [, dragConnector] = useDrag(() => ({
        type: CORE_DND_TYPE.TYPE_TREE_PRIMITIVE,
        item: new FlatDataColumnDragSource(node),
    }), [node]);
    const ref = useRef(null);
    dragConnector(ref);
    const nodeTypeIcon = _jsx(PURE_PrimitiveTypeIcon, {});
    const selectNode = () => onNodeSelect?.(node);
    const primitiveType = node.field.flatDataDataType._correspondingPrimitiveType;
    return (_jsxs("div", { ref: ref, className: "tree-view__node__container", onClick: selectNode, style: {
            paddingLeft: `${(level - 1) * (stepPaddingInRem ?? 1)}rem`,
            display: 'flex',
        }, children: [_jsx("div", { className: "tree-view__node__icon flat-data-column-tree__node__icon", children: _jsx("div", { className: "type-tree__type-icon", children: nodeTypeIcon }) }), _jsxs("div", { className: "tree-view__node__label type-tree__node__label", children: [_jsx("button", { tabIndex: -1, title: `${node.id}`, children: node.label }), _jsx("div", { className: "type-tree__node__type", children: _jsx("button", { className: clsx('type-tree__node__type__label', {
                                'type-tree__node__type__label--highlighted': primitiveType && primitiveType === selectedType,
                            }), tabIndex: -1, title: "Column Type", children: primitiveType?.path ?? 'RecordType' }) })] })] }));
};
export const FlatDataRecordTypeTree = (props) => {
    const { recordType, selectedType } = props;
    // NOTE: We only need to compute this once so we use lazy initial state syntax
    // See https://reactjs.org/docs/hooks-reference.html#lazy-initial-state
    const [treeData, setTreeData] = useState(() => getRecordTypeTreeData(recordType));
    const onNodeSelect = (node) => {
        setTreeData({ ...treeData });
    };
    const getChildNodes = (node) => [];
    useEffect(() => {
        setTreeData(() => getRecordTypeTreeData(recordType));
    }, [recordType]);
    return (_jsx(TreeView, { components: {
            TreeNodeContainer: RecordFieldTreeNodeContainer,
        }, treeData: treeData, getChildNodes: getChildNodes, onNodeSelect: onNodeSelect, innerProps: {
            selectedType,
        } }));
};
//# sourceMappingURL=FlatDataRecordTypeTree.js.map