import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import CheckCircle from "@mui/icons-material/CheckCircle";
import Error from "@mui/icons-material/Error";
import KeyboardArrowRight from "@mui/icons-material/KeyboardArrowRight";
import { styled } from "@mui/material";
import apollo_core from "@uipath/apollo-core";
import { NestedValue } from "./NestedValue.js";
const SectionArrowIcon = styled(KeyboardArrowRight, {
    shouldForwardProp: (prop)=>'expanded' !== prop
})(({ expanded })=>({
        marginRight: apollo_core.Spacing.SpacingXs,
        transition: 'transform 0.2s ease',
        cursor: 'pointer',
        transform: expanded ? 'rotate(90deg)' : 'none'
    }));
const SectionHeader = styled('div')({
    display: 'flex',
    alignItems: 'center',
    padding: apollo_core.Spacing.SpacingMicro,
    borderRadius: apollo_core.Spacing.SpacingMicro,
    cursor: 'pointer',
    '&:hover': {
        backgroundColor: 'var(--color-background-hover)'
    },
    '&:focus-visible': {
        outline: `${apollo_core.Border.BorderThickS} solid var(--color-focus-indicator)`,
        outlineOffset: '-1px'
    }
});
const SectionContent = styled('div', {
    shouldForwardProp: (prop)=>'expanded' !== prop
})(({ expanded })=>({
        marginLeft: apollo_core.Spacing.SpacingXl,
        overflow: 'hidden',
        transition: 'max-height 0.2s ease',
        maxHeight: expanded ? 'fit-content' : '0'
    }));
const KeyValueContainer = styled('div')({
    maxHeight: '180px',
    overflowY: 'auto',
    backgroundColor: 'var(--color-background-secondary)',
    padding: `${apollo_core.Spacing.SpacingXs} ${apollo_core.Spacing.SpacingS}`,
    margin: `${apollo_core.Spacing.SpacingXs} 0`,
    borderRadius: apollo_core.Spacing.SpacingMicro
});
const KeyValuePair = styled('div')({
    display: 'flex',
    marginBottom: apollo_core.Spacing.SpacingMicro,
    fontSize: apollo_core.FontFamily.FontSSize,
    '&:last-child': {
        marginBottom: 0
    }
});
const Key = styled('span')({
    fontWeight: apollo_core.FontFamily.FontWeightMedium,
    marginRight: apollo_core.Spacing.SpacingXs,
    minWidth: 'max-content'
});
const UrlValue = styled('span')({
    color: 'var(--color-foreground-link)',
    a: {
        textDecoration: 'none',
        '&:hover': {
            textDecoration: 'underline'
        }
    }
});
const CompletedStatus = styled(CheckCircle)({
    width: apollo_core.FontFamily.FontLSize,
    color: 'var(--color-success-icon)',
    fill: 'var(--color-success-icon)'
});
const WaitingStatus = styled(Error)({
    width: apollo_core.FontFamily.FontLSize,
    color: 'var(--color-warning-icon)',
    fill: 'var(--color-warning-icon)'
});
const TaskUrlKey = styled('span')({
    fontWeight: apollo_core.FontFamily.FontWeightMedium,
    marginTop: apollo_core.Spacing.SpacingMicro,
    marginRight: apollo_core.Spacing.SpacingXs,
    minWidth: 'max-content'
});
const TaskUrlsContainer = styled('div')({
    display: 'flex',
    flexDirection: 'column',
    gap: apollo_core.Spacing.SpacingXs
});
const TaskUrlItem = styled('div')({
    display: 'flex',
    alignItems: 'center',
    gap: apollo_core.Spacing.SpacingXs
});
const TaskUrlLink = styled('a')({
    color: 'inherit',
    textDecoration: 'none',
    '&:hover': {
        textDecoration: 'underline'
    },
    '&:focus-visible': {
        outline: `${apollo_core.Border.BorderThickS} solid var(--color-focus-indicator)`,
        outlineOffset: '1px'
    }
});
const StringContent = styled('div')({
    backgroundColor: 'var(--color-background-secondary)',
    padding: `${apollo_core.Spacing.SpacingXs} ${apollo_core.Spacing.SpacingS}`,
    margin: `${apollo_core.Spacing.SpacingXs} 0`,
    borderRadius: apollo_core.Spacing.SpacingMicro,
    fontSize: apollo_core.FontFamily.FontSSize,
    whiteSpace: 'pre-wrap',
    wordBreak: 'break-word',
    maxHeight: '180px',
    overflowY: 'auto'
});
const ToolCallSection = ({ title, isExpanded, onToggle, data, children })=>{
    const isStringData = 'string' == typeof data;
    const showKeyValuePairs = !isStringData && data && Object.keys(data).length > 0;
    const handleKeyDown = (event)=>{
        if ('Enter' === event.key || ' ' === event.key) {
            event.preventDefault();
            onToggle();
        }
    };
    return /*#__PURE__*/ jsxs("div", {
        children: [
            /*#__PURE__*/ jsxs(SectionHeader, {
                onClick: onToggle,
                onKeyDown: handleKeyDown,
                role: "button",
                tabIndex: 0,
                "aria-expanded": isExpanded,
                children: [
                    /*#__PURE__*/ jsx(SectionArrowIcon, {
                        fontSize: "small",
                        expanded: isExpanded
                    }),
                    title
                ]
            }),
            /*#__PURE__*/ jsxs(SectionContent, {
                expanded: isExpanded,
                "aria-hidden": !isExpanded,
                ...!isExpanded && {
                    inert: true
                },
                children: [
                    isStringData && data && /*#__PURE__*/ jsx(StringContent, {
                        children: data
                    }),
                    showKeyValuePairs && /*#__PURE__*/ jsx(KeyValueContainer, {
                        children: Object.entries(data).map(([key, value])=>/*#__PURE__*/ jsx(KeyValuePair, {
                                children: 'taskUrls' === key && Array.isArray(value) ? /*#__PURE__*/ jsxs(Fragment, {
                                    children: [
                                        /*#__PURE__*/ jsxs(TaskUrlKey, {
                                            children: [
                                                value.length > 1 ? 'taskUrls' : 'taskUrl',
                                                ":"
                                            ]
                                        }),
                                        /*#__PURE__*/ jsx(TaskUrlsContainer, {
                                            children: value.map((item, index)=>/*#__PURE__*/ jsxs(TaskUrlItem, {
                                                    children: [
                                                        /*#__PURE__*/ jsx(UrlValue, {
                                                            children: /*#__PURE__*/ jsx(TaskUrlLink, {
                                                                href: item.taskUrl,
                                                                target: "_blank",
                                                                rel: "noopener noreferrer",
                                                                children: item.taskUrl
                                                            })
                                                        }),
                                                        'unset' === item.status ? /*#__PURE__*/ jsx(WaitingStatus, {}) : /*#__PURE__*/ jsx(CompletedStatus, {})
                                                    ]
                                                }, index))
                                        })
                                    ]
                                }) : /*#__PURE__*/ jsxs(Fragment, {
                                    children: [
                                        /*#__PURE__*/ jsxs(Key, {
                                            children: [
                                                key,
                                                ":"
                                            ]
                                        }),
                                        /*#__PURE__*/ jsx(NestedValue, {
                                            value: value
                                        })
                                    ]
                                })
                            }, key))
                    }),
                    children
                ]
            })
        ]
    });
};
export { ToolCallSection };
