import md5 from 'js-md5'
import Storage from '../../utils/Storage'


isEquals=function (obj1,obj2,level = 0)
{
        if(!obj1 && !obj2)
            return true
        if(!obj1 && obj2)
            return false
        if(obj1 && !obj2)
            return false
        if(obj1 === obj2)
            return true
        let keys1 = Object.keys(obj1);
        let keys2 = Object.keys(obj2);
        if(keys1.length ==0 && keys2.length ==0 )
            return true
        if(keys1.length != keys2.length)
            return false
        let keySet = new Set([...keys1,...keys2])
        if(keySet.size != keys1.length)
            return false
        for(let key of keySet)
        {
            let temp1  = obj1[key]
            let temp2 = obj2[key]
            if(temp1 === temp2)
                continue
            if(key=='intl')
                continue
            if(typeof temp1 == 'object' && typeof temp2 == 'object' && level<=2)
                if(isEquals(temp1,temp2,level+1))
                    continue
                else
                    return false
            if(typeof temp1 == 'function' && typeof temp2 == 'function' 
                &&temp1.name=='__modelChange'    
                &&temp2.name=='__modelChange')
                continue
            
            return false

        }
        return true
}

function init()
{
    window.isEquals = isEquals
    window.ColSorter = function(a,b){
        return a > b ? 1 :a==b?0:-1
    }
    window.EN_HEX = true
    window.SHex = SHex
    window.FormatAmount = formatAmount
    window.DISPLAY_MODE = false
}
function SHex(str)
{
    if(window.EN_HEX === false)
    {
        return str
    }
    let salt = Storage.getItem('salt')
    if(salt)
        return md5(this.salt+str)
    return md5(str)
}
function formatAmount(amtstr,fix,cur){
    if(amtstr === undefined )
        return ""
    if(amtstr === null)
        return ""
    fix = fix || 2
    if(cur == 'JPY')
        fix = 2
    let amt = amtstr+''  //转字符串
    
    let idx = amt.indexOf('.')
    let segs = undefined
    if(idx > -1)
        segs = amt.split('.')
    else
        segs = [amt||'0','0'.repeat(fix)]
    if(!segs[0])
        segs[0] = '0'
    if(segs[1].length > fix)
        segs[1] = segs[1].substring(0,fix)
    else if(segs[1].length < fix)
        segs[1] = segs[1] + '0'.repeat(fix - segs[1].length)
    let npreseg = ''
    segs[0] =  segs[0].replace(/\D+/g,'')
    let preseg = segs[0]
    for(let i = 0;i < preseg.length; i++)
    {
        npreseg = preseg.charAt(preseg.length - 1 - i) + npreseg
        if(i && (1+i)%3==0 && i!=preseg.length - 1)
            npreseg = ','+ npreseg
    }
    if(cur=='JPY')
        return npreseg
    else
        return `${npreseg}.${segs[1]}`
    
}

export default {init}