import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2026 GoodData Corporation
import { useState } from "react";
import { useIntl } from "react-intl";
import { isAnalyticalBackendError, isUnexpectedResponseError, } from "@gooddata/sdk-backend-spi";
import { ErrorComponent } from "@gooddata/sdk-ui";
import { UiButton, UiCopyButton, UiIconButton, UiTooltip } from "@gooddata/sdk-ui-kit";
import { useDashboardSelector } from "../../model/react/DashboardStoreProvider.js";
import { selectPermissions } from "../../model/store/permissions/permissionsSelectors.js";
export { KdaErrorType };
var KdaErrorType;
(function (KdaErrorType) {
    KdaErrorType["Items"] = "items";
    KdaErrorType["Details"] = "details";
})(KdaErrorType || (KdaErrorType = {}));
export function KdaErrorComponent({ error }) {
    const permissions = useDashboardSelector(selectPermissions);
    const detailShow = permissions.canManageProject ?? false;
    if (isUnexpectedResponseError(error)) {
        const body = error.responseBody;
        return (_jsxs("div", { className: "gd-kda-dialog-error", children: [
                _jsx(GeneralError, {}), _jsx(DetailsShow, { detailShow: detailShow, rawError: body }), _jsx(TraceId, { traceId: error.traceId ?? body?.traceId })
            ] }));
    }
    if (isAnalyticalBackendError(error)) {
        return (_jsxs("div", { className: "gd-kda-dialog-error", children: [
                _jsx(GeneralError, {}), _jsx(DetailsShow, { detailShow: detailShow })
            ] }));
    }
    return _jsx(GeneralError, {});
}
function GeneralError() {
    const intl = useIntl();
    return (_jsx(ErrorComponent, { height: "auto", message: intl.formatMessage({ id: "kdaDialog.dialog.keyDrives.error.general.title" }), description: intl.formatMessage({
            id: "kdaDialog.dialog.keyDrives.error.general.description",
        }) }));
}
function TraceId({ traceId }) {
    const intl = useIntl();
    if (!traceId) {
        return null;
    }
    return (_jsxs("div", { className: "gd-kda-dialog-error-trace-id", children: [
            _jsxs("div", { children: [
                    _jsxs("span", { className: "gd-kda-dialog-error-trace-id-name", children: [intl.formatMessage({ id: "kdaDialog.dialog.keyDrives.error.traceId" }), ":"] }), " ", traceId] }), _jsx(UiCopyButton, { label: intl.formatMessage({ id: "kdaDialog.dialog.keyDrives.error.traceId.copy" }), clipboardContent: traceId ?? "" })
        ] }));
}
function DetailsShow({ detailShow, rawError }) {
    const intl = useIntl();
    const [open, setOpen] = useState(false);
    if (!detailShow || !rawError) {
        return null;
    }
    return (_jsx("div", { className: "gd-kda-dialog-error-details-show", children: open ? (_jsxs("div", { className: "gd-kda-dialog-error-details-show-content", children: [
                _jsxs("div", { className: "gd-kda-dialog-error-details-show-content-title", children: [
                        _jsx("div", { className: "gd-kda-dialog-error-details-show-content-title-text", children: intl.formatMessage({ id: "kdaDialog.dialog.keyDrives.error.details.title" }) }), _jsx(UiTooltip, { triggerBy: ["hover", "focus"], anchor: _jsx(UiIconButton, { icon: "question", variant: "tertiary" }), content: intl.formatMessage({
                                id: "kdaDialog.dialog.keyDrives.error.details.title.tooltip",
                            }) })
                    ] }), _jsx("div", { className: "gd-kda-dialog-error-details-show-content-description", children: JSON.stringify(rawError) })
            ] })) : (_jsx(UiButton, { label: "See the raw error response for details", variant: "popout", size: "medium", onClick: () => {
                setOpen(true);
            } })) }));
}
//# sourceMappingURL=KdaErrorComponent.js.map