import Cropper from './cropper.vue'
import { mount } from '../../utils/componentHelper'
import { toRawType } from '../../utils/index'

Cropper.use = function use(app) {
    const isCropper = false
    Cropper.open = function (config) {
        if (isCropper) {
            return Promise.reject('正在裁剪')
        }
        if (toRawType(config) === 'String') {
            config = { img: config }
        }
        if (!config || !config.img) {
            return Promise.reject('请传入裁剪图片')
        }

        return new Promise((resolve, reject) => {
            const { destroy } = mount(Cropper, {
                app,
                props: {
                    ...config,
                    cb(err, data) {
                        destroy()
                        if (err) {
                            reject(err)
                        } else {
                            resolve(data)
                        }
                    },
                },
            })
        })
    }
}

export default Cropper
