import { jsx as _jsx } from "react/jsx-runtime";
import { memo, useCallback } from 'react';
import { useAtomValue } from 'jotai';
import { PayloadSamples, StyledCodeBlock } from '../PayloadSamples/index.js';
import { createCodeSampleCopyEvent } from '../../events/index.js';
import { globalOptionsAtom } from '../../jotai/store.js';
import { Summary } from './Summary.js';
import { Tabs } from '../Tabs/index.js';
import { CodeBlockPanel } from '../common/index.js';
import { useTelemetry, useTranslate } from '../../hooks/index.js';
import { activeMimeNameAtom } from '../../jotai/app.js';
import { operationStore } from '../../jotai/operation.js';
function ResponseSamplesComponent({ operation, activeResponseTab, onTabChange, onPanelToggle, }) {
    const telemetry = useTelemetry();
    const { events } = useAtomValue(globalOptionsAtom);
    const translate = useTranslate();
    const responses = operation.responses.filter((response) => response.content?.hasSample ?? false);
    const tabs = operation.responses.map(({ code }) => ({ key: code, title: code }));
    const activeTab = tabs?.find(({ key }) => key === activeResponseTab) || tabs?.[0];
    const activeMimeName = useAtomValue(activeMimeNameAtom);
    const { activeExampleName } = useAtomValue(operationStore(operation.pointer));
    const handleCodeSampleCopy = useCallback(() => {
        telemetry.send({
            type: 'openapi_docs.copy_code_snippet.clicked',
            payload: { snippetType: 'response' },
        });
        const event = createCodeSampleCopyEvent({
            operation,
            type: 'response',
            activeMimeName,
            activeExampleName,
        });
        events?.codeSamplesCopy?.(event);
    }, [telemetry, operation, events, activeMimeName, activeExampleName]);
    const handlePanelToggle = useCallback((isExpanded) => {
        onPanelToggle?.(isExpanded, 'response-samples');
    }, [onPanelToggle]);
    if (!responses.length) {
        return null;
    }
    const renderSummary = () => {
        return (_jsx(Summary, { tabs: _jsx(Tabs, { tabs: tabs, activeTab: activeTab, onChange: (tab) => onTabChange(tab.key) }) }));
    };
    return (_jsx(CodeBlockPanel, { className: "panel-response-samples", header: renderSummary, isExpandable: false, children: operation.responses.map((response) => response.code === activeTab.key ? (_jsx("div", { children: response?.content?.hasSample ? (_jsx(PayloadSamples, { content: response.content, onCopyClick: handleCodeSampleCopy, onPanelToggle: handlePanelToggle })) : (_jsx(StyledCodeBlock, { source: translate('openapi.noResponseContent', 'No content') })) }, response.code)) : null) }));
}
export const ResponseSamples = memo(ResponseSamplesComponent);
//# sourceMappingURL=ResponseSamples.js.map