import { useEffect } from "react"
import { usePage } from "@inertiajs/react"
import useFormBuilder from "@/utils/useFormBuilder"
import FormBuilder from "@/Components/FormBuilder"
import { slugify, useSearchParams } from "@/utils/format"

function MainForm({ isEdit = false, formOptions = {}, BackButton = null }) {
  const formConfig = { isEdit, getProperties, formOptions }
  const builderProps = useFormBuilder(formConfig)
  const { data, setData } = builderProps.form

  return <FormBuilder {...builderProps} BackButton={BackButton} />
}

const getProperties = (state) => {
  const { props, data, setData } = state
  const { params } = useSearchParams(props.location)

  const properties = [
    [
      {
        form: "input",
        props: {
          name: "module",
          label: "Modul",
          type: "text",
        },
      },
      {
        form: "input",
        props: {
          name: "prefix",
          label: "Prefix",
          type: "text",
        },
      },
    ],
    [
      {
        form: "input",
        props: {
          name: "separator",
          label: "Pemisah",
          type: "text",
        },
      },
      {
        form: "input",
        props: {
          name: "digits",
          label: "Digit",
          type: "number",
        },
      },
    ],
    [
      {
        form: "input",
        props: {
          name: "last_number",
          label: "Nomor Terakhir",
          type: "number",
        },
      },
      {
        form: "checkbox",
        props: {
          name: "is_user_alias",
          label: "Gunakan Alias Pengguna",
        },
      },
    ],
  ]

  return properties
}

const PropertiesProvider = ({ children }) => {
  return children(getProperties)
}

MainForm.PropertiesProvider = PropertiesProvider

const CodeConfigMainForm = MainForm
export default CodeConfigMainForm
