import React, { useEffect, useMemo, useRef, useState } from 'react' import { Head, Link, router, useForm } from '@inertiajs/react' import { createPortal } from 'react-dom' import AdminLayout from '../../../Layouts/AdminLayout' import DateTimePicker from '../../../components/ui/DateTimePicker' import NovaSelect from '../../../components/ui/NovaSelect' import ShareToast from '../../../components/ui/ShareToast' import ChallengeEditor from './ChallengeEditor' import CourseEditor from './CourseEditor' import LessonEditor from './LessonEditor' function normalizePayload(fields, data) { const payload = { ...data } fields.forEach((field) => { if (field.type === 'csv') { payload[field.name] = String(payload[field.name] || '') .split(/[,\n]/) .map((item) => item.trim()) .filter(Boolean) } if (field.type === 'json') { if (typeof payload[field.name] === 'string') { const trimmed = payload[field.name].trim() if (!trimmed) { payload[field.name] = null return } try { payload[field.name] = JSON.parse(trimmed) } catch { // Keep the original string so the caller can surface a field-specific validation error. } } } }) return payload } function serializeStructuredJson(value) { if (value == null || value === '') return '' if (typeof value === 'string') return value try { return JSON.stringify(value, null, 2) } catch { return '' } } function copyTextToClipboard(text) { const source = String(text || '') if (!source) return Promise.reject(new Error('Nothing to copy')) if (typeof navigator !== 'undefined' && navigator.clipboard && typeof navigator.clipboard.writeText === 'function') { return navigator.clipboard.writeText(source) } if (typeof document === 'undefined' || !document.body) { return Promise.reject(new Error('Clipboard unavailable')) } const textarea = document.createElement('textarea') textarea.value = source textarea.setAttribute('readonly', 'true') textarea.style.position = 'fixed' textarea.style.top = '-1000px' textarea.style.left = '-1000px' document.body.appendChild(textarea) textarea.select() try { if (document.execCommand('copy')) { return Promise.resolve() } } finally { document.body.removeChild(textarea) } return Promise.reject(new Error('Clipboard unavailable')) } function getField(fields, name) { return fields.find((field) => field.name === name) || null } function firstErrorMessage(errors, fallback = 'Please correct the highlighted fields and try again.') { const queue = [errors] while (queue.length > 0) { const current = queue.shift() if (typeof current === 'string') { const message = current.trim() if (message) { return message } continue } if (Array.isArray(current)) { queue.push(...current) continue } if (current && typeof current === 'object') { queue.push(...Object.values(current)) } } return fallback } function SectionCard({ eyebrow, title, description, children, className = '' }) { return (
{eyebrow ?

{eyebrow}

: null}

{title}

{description ?

{description}

: null}
{children}
) } function TextField({ label, value, onChange, error, ...rest }) { return ( ) } function TextAreaField({ label, value, onChange, error, rows = 6, hint }) { return (