import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
import { memo } from 'react';
import { useAtom } from 'jotai';
import { StyledBadge, SelectionTitle } from '../common/index.js';
import { Markdown } from '../Markdown/index.js';
import { ConstraintsView } from '../common/ConstraintsView.js';
import { Schema } from './Schema.js';
import { operationStore } from '../../jotai/operation.js';
import { SchemaSelection } from '../common/SchemaSelection/index.js';
import { useOneOfLocationIdx } from './useOneOfLocationIdx.js';
import { useTranslate } from '../../hooks/index.js';
import { styled } from '../../styled-components.js';
function OneOfSchemaComponent({ schema: { oneOf }, schema, onChange, oneOfLevel = 1, ...rest }) {
    const translate = useTranslate();
    const [store, setOperationStore] = useAtom(operationStore(schema.operationPointer));
    const oneOfIdxLocation = useOneOfLocationIdx(oneOf, oneOfLevel);
    const activeOneOfIdx = oneOfIdxLocation === -1 ? 0 : oneOfIdxLocation;
    const activeSchemaIndex = store.activeOneOf?.[schema.pointer] !== undefined
        ? store.activeOneOf[schema.pointer]
        : activeOneOfIdx;
    const activeSchema = oneOf[activeSchemaIndex];
    if (!activeSchema) {
        return null;
    }
    const options = oneOf.map((subSchema, idx) => ({
        label: subSchema.title || subSchema.typePrefix + subSchema.displayType,
        value: idx,
    }));
    const handleChange = (value) => {
        onChange?.({
            pointer: schema.pointer,
            index: value,
        });
        setOperationStore({
            activeExampleName: schema.oneOf?.[value]?.title,
            activeOneOf: { [schema.pointer]: value },
            requestValues: { body: null },
        });
    };
    return (_jsxs(Wrapper, { children: [_jsxs(SelectionTitle, { children: [schema.oneOfType, ":"] }), _jsx(SchemaSelection, { options: options, onChange: handleChange, pointer: schema.operationPointer, schema: schema, defaultOneOfIdx: activeOneOfIdx }), activeSchema.deprecated && (_jsx(StyledBadge, { deprecated: true, children: translate('openapi.badges.deprecated', 'Deprecated') })), activeSchema.description && _jsx(StyledDescription, { source: activeSchema.description }), _jsx(ConstraintsView, { constraints: activeSchema.constraints }), _jsx(Schema, { ...rest, schema: activeSchema, oneOfLevel: oneOfLevel + 1 })] }));
}
export const OneOfSchema = memo(OneOfSchemaComponent);
const StyledDescription = styled(Markdown) `
  margin-top: var(--spacing-xs);
  font-size: var(--font-size-base);
  line-height: var(--line-height-base);
`;
const Wrapper = styled.div `
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  width: 100%;
`;
//# sourceMappingURL=OneOfSchema.js.map