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: "code",
          label: "Kode",
          type: "text",
        },
      },
      {
        form: "input",
        props: {
          name: "name",
          label: "Nama",
          type: "text",
        },
      },
    ],
    [
      {
        form: "input",
        props: {
          name: "address",
          label: "Alamat",
          type: "text",
        },
      },
      {
        form: "select",
        props: {
          name: "status",
          label: "Status",
          options: props.status,
          prefix: "company.status",
        },
      },
    ],
  ]

  return properties
}

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

MainForm.PropertiesProvider = PropertiesProvider

const CompanyMainForm = MainForm
export default CompanyMainForm
