import Vue from 'vue'
import Main from './main.vue'
import { hasOwn } from '../../utils'

const EditFormConstructor = Vue.extend(Main)
const FORM_PROPS = Object.keys(Main.props)

let instance

const EditForm = function EditForm(options = {}) {
  if (!instance) {
    instance = new EditFormConstructor()
    instance.$mount()
  }

  const formProps = {}
  for (let prop in options) {
    if (hasOwn(options, prop)) {
      if (FORM_PROPS.includes(prop)) {
        instance[prop] = options[prop]
      } else {
        formProps[prop] = options[prop]
      }
    }
  }

  instance.formProps = formProps

  Vue.nextTick(() => {
    instance.visible = true
  })

  return instance
}

export { EditForm }
