import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "@emotion/react/jsx-runtime";
import { getStatePath, getSubstatePath } from '../../utils/paths';
import { ListItemInitialStyled, ListItemSuffixStyled, } from './state-list.style';
const parseSubstate = ({ id, name, x, y, }, path, parent) => ({
    key: id,
    title: `${name} (x: ${x}${y !== undefined ? `, y: ${y}` : ''})`,
    isLeaf: true,
    parent,
    path: path.concat('substates', `id:${id}`),
});
export const parseStates = (states, path, initialState, initialSuffix) => states.map((state) => {
    var _a;
    const statePath = path.concat('states', `id:${state.id}`);
    const node = {
        key: state.id,
        title: () => {
            if (state.id === initialState) {
                return (_jsxs(_Fragment, { children: [_jsx(ListItemInitialStyled, { children: state.name }), _jsx(ListItemSuffixStyled, { children: initialSuffix })] }));
            }
            return state.name;
        },
        path: statePath,
    };
    node.children = (_a = state === null || state === void 0 ? void 0 : state.substates) === null || _a === void 0 ? void 0 : _a.map((substate) => parseSubstate(substate, statePath, node));
    return node;
});
export const getSelectedPaths = (paths, inspectedEntity) => {
    const selectedPaths = paths.filter((path) => path.at(-2) === 'states' || path.at(-2) === 'substates');
    if (!selectedPaths.length && ((inspectedEntity === null || inspectedEntity === void 0 ? void 0 : inspectedEntity.type) === 'transition' || (inspectedEntity === null || inspectedEntity === void 0 ? void 0 : inspectedEntity.type) === 'frame')) {
        const statePath = getStatePath(inspectedEntity.path);
        const substatePath = getSubstatePath(inspectedEntity.path);
        const selectedPath = substatePath !== null && substatePath !== void 0 ? substatePath : statePath;
        return selectedPath ? [selectedPath] : [];
    }
    return selectedPaths;
};
