'use client';
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useState } from 'react';
import { CaretDown, CaretRight, Plus, Minus } from '@phosphor-icons/react';
function getTypeLabel(prop) {
    if (prop.type === 'array' && prop.items) {
        const itemType = prop.items.type || 'object';
        return `${itemType}[]`;
    }
    return prop.type || 'object';
}
function hasChildProperties(prop) {
    if (prop.properties && Object.keys(prop.properties).length > 0) {
        return true;
    }
    if (prop.type === 'array' && prop.items?.properties) {
        return true;
    }
    if (prop.allOf || prop.oneOf || prop.anyOf) {
        return true;
    }
    return false;
}
function PropertyRow({ name, prop, isRequired, level = 0 }) {
    const [showChildren, setShowChildren] = useState(false);
    const hasChildren = hasChildProperties(prop);
    const typeLabel = getTypeLabel(prop);
    // Get child properties
    const getChildProperties = ()=>{
        if (prop.properties) {
            return prop.properties;
        }
        if (prop.type === 'array' && prop.items?.properties) {
            return prop.items.properties;
        }
        if (prop.allOf) {
            // Merge all schemas
            const merged = {};
            prop.allOf.forEach((schema)=>{
                if (schema.properties) {
                    Object.assign(merged, schema.properties);
                }
            });
            return Object.keys(merged).length > 0 ? merged : null;
        }
        return null;
    };
    const getChildRequired = ()=>{
        if (prop.required) return prop.required;
        if (prop.type === 'array' && prop.items?.required) {
            return prop.items.required;
        }
        if (prop.allOf) {
            const allRequired = [];
            prop.allOf.forEach((schema)=>{
                if (schema.required) {
                    allRequired.push(...schema.required);
                }
            });
            return allRequired;
        }
        return [];
    };
    const childProps = getChildProperties();
    const childRequired = getChildRequired();
    return /*#__PURE__*/ _jsxs("div", {
        className: `${level > 0 ? 'border-l-2 border-muted pl-4 ml-2' : ''}`,
        children: [
            /*#__PURE__*/ _jsxs("div", {
                className: "py-3 border-b border-border/50",
                children: [
                    /*#__PURE__*/ _jsxs("div", {
                        className: "flex items-start gap-2 flex-wrap",
                        children: [
                            /*#__PURE__*/ _jsx("span", {
                                className: "font-semibold text-sm text-foreground",
                                children: name
                            }),
                            /*#__PURE__*/ _jsx("span", {
                                className: "text-xs text-muted-foreground",
                                children: typeLabel
                            }),
                            prop.format && /*#__PURE__*/ _jsxs(_Fragment, {
                                children: [
                                    /*#__PURE__*/ _jsx("span", {
                                        className: "text-xs text-muted-foreground",
                                        children: "·"
                                    }),
                                    /*#__PURE__*/ _jsx("span", {
                                        className: "text-xs text-muted-foreground",
                                        children: prop.format
                                    })
                                ]
                            }),
                            isRequired && /*#__PURE__*/ _jsx("span", {
                                className: "text-xs text-orange-500 font-medium",
                                children: "required"
                            }),
                            prop.example !== undefined && /*#__PURE__*/ _jsx("button", {
                                className: "text-xs text-muted-foreground hover:text-foreground underline",
                                children: "Example"
                            })
                        ]
                    }),
                    prop.description && /*#__PURE__*/ _jsx("p", {
                        className: "text-sm text-muted-foreground mt-1",
                        children: prop.description
                    }),
                    prop.enum && /*#__PURE__*/ _jsxs("div", {
                        className: "mt-2 flex flex-wrap gap-1",
                        children: [
                            /*#__PURE__*/ _jsx("span", {
                                className: "text-xs text-muted-foreground",
                                children: "Enum:"
                            }),
                            prop.enum.map((val, idx)=>/*#__PURE__*/ _jsx("code", {
                                    className: "text-xs bg-muted px-1.5 py-0.5 rounded",
                                    children: JSON.stringify(val)
                                }, idx))
                        ]
                    }),
                    prop.default !== undefined && /*#__PURE__*/ _jsxs("div", {
                        className: "mt-1",
                        children: [
                            /*#__PURE__*/ _jsx("span", {
                                className: "text-xs text-muted-foreground",
                                children: "Default: "
                            }),
                            /*#__PURE__*/ _jsx("code", {
                                className: "text-xs bg-muted px-1.5 py-0.5 rounded",
                                children: JSON.stringify(prop.default)
                            })
                        ]
                    }),
                    hasChildren && childProps && /*#__PURE__*/ _jsx("button", {
                        onClick: ()=>setShowChildren(!showChildren),
                        className: "mt-2 flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground border rounded-full px-3 py-1 hover:bg-muted/50 transition-colors",
                        children: showChildren ? /*#__PURE__*/ _jsxs(_Fragment, {
                            children: [
                                /*#__PURE__*/ _jsx(Minus, {
                                    className: "h-3 w-3",
                                    weight: "bold"
                                }),
                                "Hide Child Attributes"
                            ]
                        }) : /*#__PURE__*/ _jsxs(_Fragment, {
                            children: [
                                /*#__PURE__*/ _jsx(Plus, {
                                    className: "h-3 w-3",
                                    weight: "bold"
                                }),
                                "Show Child Attributes"
                            ]
                        })
                    })
                ]
            }),
            showChildren && childProps && /*#__PURE__*/ _jsx("div", {
                className: "mt-2",
                children: Object.entries(childProps).map(([childName, childProp])=>/*#__PURE__*/ _jsx(PropertyRow, {
                        name: childName,
                        prop: childProp,
                        isRequired: childRequired.includes(childName),
                        level: level + 1
                    }, childName))
            })
        ]
    });
}
export function SchemaViewer({ schema, requiredFields = [], level = 0 }) {
    // Parse schema if it's a string
    let parsedSchema;
    try {
        if (typeof schema === 'string') {
            parsedSchema = JSON.parse(schema);
        } else {
            parsedSchema = schema;
        }
    } catch  {
        // If parsing fails, show raw content
        return /*#__PURE__*/ _jsx("pre", {
            className: "text-xs text-muted-foreground p-3 bg-muted/50 rounded overflow-auto",
            children: typeof schema === 'string' ? schema : JSON.stringify(schema, null, 2)
        });
    }
    // Handle nested schema property (common in OpenAPI responses)
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    if (parsedSchema.schema) {
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        parsedSchema = parsedSchema.schema;
    }
    // Handle allOf, oneOf, anyOf at root level
    if (parsedSchema.allOf) {
        const mergedProps = {};
        const mergedRequired = [];
        parsedSchema.allOf.forEach((s)=>{
            if (s.properties) Object.assign(mergedProps, s.properties);
            if (s.required) mergedRequired.push(...s.required);
        });
        parsedSchema = {
            ...parsedSchema,
            properties: mergedProps,
            required: mergedRequired
        };
    }
    const properties = parsedSchema.properties || {};
    const required = parsedSchema.required || requiredFields;
    if (Object.keys(properties).length === 0) {
        // No properties - maybe it's a simple type
        if (parsedSchema.type) {
            return /*#__PURE__*/ _jsxs("div", {
                className: "py-2",
                children: [
                    /*#__PURE__*/ _jsx("span", {
                        className: "text-xs text-muted-foreground",
                        children: parsedSchema.type
                    }),
                    parsedSchema.description && /*#__PURE__*/ _jsx("p", {
                        className: "text-sm text-muted-foreground mt-1",
                        children: parsedSchema.description
                    })
                ]
            });
        }
        return /*#__PURE__*/ _jsx("pre", {
            className: "text-xs text-muted-foreground p-3 bg-muted/50 rounded overflow-auto",
            children: JSON.stringify(parsedSchema, null, 2)
        });
    }
    return /*#__PURE__*/ _jsx("div", {
        className: level > 0 ? 'pl-4 border-l-2 border-muted' : '',
        children: Object.entries(properties).map(([name, prop])=>/*#__PURE__*/ _jsx(PropertyRow, {
                name: name,
                prop: prop,
                isRequired: required.includes(name),
                level: level
            }, name))
    });
}
export function ResponseSchema({ code, status, description, schema, contentType = 'application/json' }) {
    const [isOpen, setIsOpen] = useState(code >= 200 && code < 300);
    const [viewMode, setViewMode] = useState('schema');
    // Try to parse schema
    let parsedSchema = null;
    try {
        if (typeof schema === 'string') {
            parsedSchema = JSON.parse(schema);
        } else {
            parsedSchema = schema;
        }
        // Handle nested schema property
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        if (parsedSchema && parsedSchema.schema) {
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            parsedSchema = parsedSchema.schema;
        }
    } catch  {
        parsedSchema = null;
    }
    const typeLabel = parsedSchema?.type || 'object';
    return /*#__PURE__*/ _jsxs("div", {
        className: "border-b border-border",
        children: [
            /*#__PURE__*/ _jsxs("button", {
                onClick: ()=>setIsOpen(!isOpen),
                className: "w-full py-3 flex items-center gap-3 hover:bg-muted/30 transition-colors text-left",
                children: [
                    isOpen ? /*#__PURE__*/ _jsx(CaretDown, {
                        className: "h-4 w-4 text-muted-foreground shrink-0",
                        weight: "bold"
                    }) : /*#__PURE__*/ _jsx(CaretRight, {
                        className: "h-4 w-4 text-muted-foreground shrink-0",
                        weight: "bold"
                    }),
                    /*#__PURE__*/ _jsx("span", {
                        className: `font-semibold text-sm ${code >= 200 && code < 300 ? 'text-green-600' : code >= 400 ? 'text-red-500' : 'text-orange-500'}`,
                        children: code
                    }),
                    /*#__PURE__*/ _jsx("span", {
                        className: "text-sm text-muted-foreground truncate",
                        children: description || status
                    })
                ]
            }),
            isOpen && /*#__PURE__*/ _jsxs("div", {
                className: "pb-4 pl-7",
                children: [
                    /*#__PURE__*/ _jsxs("div", {
                        className: "flex items-center justify-between mb-3",
                        children: [
                            /*#__PURE__*/ _jsxs("div", {
                                className: "flex items-center gap-2",
                                children: [
                                    /*#__PURE__*/ _jsx("span", {
                                        className: "text-xs text-muted-foreground",
                                        children: typeLabel
                                    }),
                                    /*#__PURE__*/ _jsx("button", {
                                        onClick: ()=>setViewMode(viewMode === 'schema' ? 'example' : 'schema'),
                                        className: `text-xs underline ${viewMode === 'example' ? 'text-foreground' : 'text-muted-foreground hover:text-foreground'}`,
                                        children: "Example"
                                    })
                                ]
                            }),
                            /*#__PURE__*/ _jsx("span", {
                                className: "text-xs text-muted-foreground font-mono",
                                children: contentType
                            })
                        ]
                    }),
                    viewMode === 'schema' && parsedSchema ? /*#__PURE__*/ _jsx(SchemaViewer, {
                        schema: parsedSchema
                    }) : /*#__PURE__*/ _jsx("pre", {
                        className: "text-xs bg-muted/50 p-3 rounded overflow-auto",
                        children: typeof schema === 'string' ? schema : JSON.stringify(schema, null, 2)
                    })
                ]
            })
        ]
    });
}
