import PostOriginalCreatedTimeField from '@/components/PostOriginalCreatedTimeField'
import PostTagsField from '@/components/posts/PostTagsField'
import PostTextField from '@/components/posts/PostTextField'

import type { FC, ReactNode } from 'react'

type TextMessages = string[] | undefined

type CoreField = {
  value:    string
  onChange: (value: string) => void
  errors?:  TextMessages
  warnings?: TextMessages
  disabled?: boolean }

type OriginalCreatedField = {
  originalCreatedAt?:     TextMessages
  originalCreatedFrom?:   TextMessages
  originalCreatedBefore?: TextMessages }

type PostCoreDataFieldsProps = {
  title: {
    value:     string
    onChange:  (value: string) => void
    errors?:   TextMessages
    warnings?: TextMessages
    disabled?: boolean
    after?:    ReactNode }
  originalCreated: {
    disabled?:                boolean
    originalCreatedFrom:      string | null
    setOriginalCreatedFrom:   (value: string | null) => void
    originalCreatedBefore:    string | null
    setOriginalCreatedBefore: (value: string | null) => void
    errors?:                  OriginalCreatedField }
  tags: {
    value:     string
    onChange:  (value: string) => void
    errors?:   TextMessages
    warnings?: TextMessages
    disabled?: boolean
    rows?:     number }
  parentPostIds: CoreField }

const groupedMessages = (...values: (TextMessages | null | undefined)[]): string[] =>
    [...new Set (values.flatMap (value => value ?? []))]


const PostCoreDataFields: FC<PostCoreDataFieldsProps> = (
    { title,
      originalCreated,
      tags,
      parentPostIds },
) => (
    <>
      <PostTextField
          label="タイトル"
          value={title.value}
          disabled={title.disabled}
          warnings={title.warnings}
          errors={title.errors}
          after={title.after}
          onChange={title.onChange}/>

      <PostOriginalCreatedTimeField
          disabled={originalCreated.disabled}
          originalCreatedFrom={originalCreated.originalCreatedFrom}
          setOriginalCreatedFrom={originalCreated.setOriginalCreatedFrom}
          originalCreatedBefore={originalCreated.originalCreatedBefore}
          setOriginalCreatedBefore={originalCreated.setOriginalCreatedBefore}
          errors={groupedMessages (
              originalCreated.errors?.originalCreatedAt,
              originalCreated.errors?.originalCreatedFrom,
              originalCreated.errors?.originalCreatedBefore)}/>

      <PostTagsField
          tags={tags.value}
          disabled={tags.disabled}
          setTags={tags.onChange}
          warnings={tags.warnings}
          errors={tags.errors}
          rows={tags.rows}/>

      <PostTextField
          label="親投稿"
          value={parentPostIds.value}
          disabled={parentPostIds.disabled}
          warnings={parentPostIds.warnings}
          errors={parentPostIds.errors}
          onChange={parentPostIds.onChange}/>
    </>)

export default PostCoreDataFields
export type { PostCoreDataFieldsProps }
