import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { memo } from 'react';
import { useAtom } from 'jotai';
import { ExampleSwitch, useExampleKey } from '../Samples/index.js';
import { useActivateExample, useTelemetry } from '../../hooks/index.js';
import { operationStore } from '../../jotai/operation.js';
function ExampleComponent({ mediaType, mediaContent, renderSample }) {
    const telemetry = useTelemetry();
    const pointer = mediaType.operation.pointer;
    const [operation, setOperation] = useAtom(operationStore(pointer));
    const examples = mediaType.examples || {};
    const examplesKeys = Object.keys(examples);
    const setActivateExampleName = useActivateExample(mediaContent);
    const { exampleKey } = useExampleKey(mediaType.operation, examples);
    if (!examplesKeys.length || examplesKeys.length === 1) {
        return renderSample();
    }
    const handleExampleChange = (key) => {
        telemetry.send({
            type: 'openapi_docs.examples_switcher.clicked',
            payload: {
                example: key,
                numberOfExamples: examplesKeys.length,
            },
        });
        setOperation({
            ...operation,
            activeExampleName: key,
            activeOneOf: { [pointer]: examplesKeys.indexOf(key) },
            requestValues: { body: null },
        });
        setActivateExampleName(key);
    };
    return (_jsxs(_Fragment, { children: [_jsx(ExampleSwitch, { examples: examples, exampleKey: exampleKey, onChange: handleExampleChange }), renderSample(exampleKey)] }));
}
export const Example = memo(ExampleComponent);
//# sourceMappingURL=Example.js.map