export * from "./eventify"
export * from "./extend"
export * from "./usesify"
export * from "./parser"
export * from "./style"

import { forin } from './style'

export function type(object: any) {
    const class2type = {}
    const type = class2type.toString.call(object)
    const typeString = 'Boolean Number String Function Array Date RegExp Object Error Symbol'

    if (object == null) return object + ''

    typeString.split(' ').forEach((type) => {
        class2type[`[object ${type}]`] = type.toLowerCase()
    })

    const isObject = typeof object === 'object'
    const isFn = typeof object === 'function'
    return isObject || isFn ? class2type[type] || 'object' : typeof object
}

export const getValue = (root: any, get: string) => {
    if (type(root) !== 'object') return root
    let value = root
    const keyArr = get.split('.')
    for (let i = 0, l = keyArr.length; i < l; i++) {
        const v = keyArr[i]
        if (v) {
            if (value[v]) {
                value = value[v]
            } else {
                value = undefined
                break
            }
        }
    }
    return value
}

export const setValue = (tar: any, key: string, value: any) => {
    if (type(tar) !== 'object') {
        error('setValue tar muse be a object!')
    } else {
        const pIndex = key.trim().indexOf('.')
        if (pIndex > 0 && pIndex < key.length - 1) {
            const keyArr = key.trim().split('.')
            let _obj = tar
            let wrongKey = ''
            for (let i = 0, l = keyArr.length - 1; i < l; i++) {
                const v = keyArr[i]
                if (typeof _obj[v] === 'object') {
                    _obj = _obj[v]
                } else if (typeof _obj[v] === 'undefined') {
                    wrongKey = v
                }
            }

            if (!wrongKey) {
                const lastKey = keyArr[keyArr.length - 1]
                _obj[lastKey] = value
            } else {
                error(`the key(${wrongKey}) is not in tar obj!`)
            }
        } else {
            tar[key.replace(/\./g, '')] = value
        }
    }
}

export const getCoverRect = (target: any) => {
    if (target.getGlobalBounds && target.layout) {
        const { width: cw, height: ch } = target.getGlobalBounds()
        const { x: scaleX, y: scaleY } = target.layout.realScale
        const width = cw / scaleX
        const height = ch / scaleY
        const x = - width / 2
        const y = - height / 2

        return { x, y, width, height }
    } else {
        return null
    }
}

export function mountApi(instance: any, from: string, apis: Array<string | { [key: string]: string }>) {
    const _fromObj = getValue(instance, from)
    apis.map((api: string | object) => {
        if (typeof api === 'string') {
            instance[api] = (...extra) => _fromObj[api](...extra)
        } else if (typeof api === 'object') {
            forin(api, (key: string, value: string) => {
                instance[key] = (...extra) => _fromObj[value](...extra)
            })
        }
        
    })
}

export function error(msg: string) {
    throw Error(`[@amoy/components]error: ${msg};`)
}

export function warn(msg: string) {
    console.warn(`[@amoy/components]warn: ${msg};`)
}
