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

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

        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
