/**
 * CreateDialog — Primer Dialog wrapper around CreateArtifactForm.
 *
 * The form, fields, validation, data-loading, and submission live in
 * `CreateArtifactForm`. This component just supplies the modal chrome.
 */
import Dialog from '../Dialog.jsx'
import CreateArtifactForm, { ARTIFACT_SCHEMAS } from './CreateArtifactForm.jsx'
import { resolveSchemaKey, TYPE_KEY_MAP } from './createHelpers.js'

export default function CreateDialog({ type, basePath, onClose }) {
  // `type === null`/`undefined` means "no dialog requested" — render nothing.
  // Explicit `'Any'` (or empty string) means picker mode.
  if (type == null) return null

  const schemaKey = resolveSchemaKey(type)
  const isPicker = schemaKey === null
  const schema = schemaKey ? ARTIFACT_SCHEMAS[schemaKey] : null

  if (!isPicker && !schema) return null

  return (
    <Dialog
      title={isPicker ? 'New artifact' : `New ${schema.label}`}
      subtitle={isPicker ? 'Pick an artifact type to create.' : schema.description}
      onClose={onClose}
      width="large"
    >
      <CreateArtifactForm
        type={type}
        basePath={basePath}
        onClose={onClose}
        hideHeader
      />
    </Dialog>
  )
}

// Re-export for canvas/workspace integrations
export { ARTIFACT_SCHEMAS as CREATE_SCHEMAS, TYPE_KEY_MAP }
