import { useRef } from "react"
import { ObjectArrayEditorHandler, ObjectArrayEditor, ObjectArrayEditorProps } from "./ObjectArrayEditor"

import { EditorProperties } from "@iplusplus/y-model"
import { DraggableDialog } from "../ui-utils"


type ObjectArrayEditorDialogProps = Omit<ObjectArrayEditorProps, "refreshByData"> & {
  onClose: (value?: Record<string, unknown>[]) => void
  title?: string
}

export function ObjectArrayEditorDialog(props: ObjectArrayEditorDialogProps) {
  const { onClose, title,...editorPorps } = props;
  const handler = useRef<ObjectArrayEditorHandler>(null)


  const handleCancel = () => {
    onClose()
  }

  const handleOk = () => {
    onClose(handler.current?.getData())
  }

  return (
    <DraggableDialog initialWidth="lg" title={title || "编辑清单属性"} onOk={handleOk} onCancel={handleCancel}>
      <ObjectArrayEditor
        ref={handler}
        {...editorPorps}
      />
    </DraggableDialog>
  )
}
