
import Codes from "./enum/Codes.js"
import CodeErrorMap from "./error/CodeErrorMap.js"
import InternalError from "./error/InternalError.js"

var util = {
    uuid() {
        return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, i=>{
            const e = Math.random() * 16 | 0;
            return (i === "x" ? e : e & 3 | 8).toString(16)
        }
        )
    },
    getFormattedDate(i) {
        const e = i.getMonth() + 1
          , t = i.getDate()
          , r = i.getHours()
          , n = i.getMinutes()
          , o = i.getSeconds()
          , a = i.getMilliseconds()
          , s = (e < 10 ? "0" : "") + e
          , l = (t < 10 ? "0" : "") + t
          , u = (r < 10 ? "0" : "") + r
          , c = (n < 10 ? "0" : "") + n
          , h = (o < 10 ? "0" : "") + o;
        return i.getFullYear() + "-" + s + "-" + l + " " + u + ":" + c + ":" + h + "." + a
    },
    createInstance(i) {
        var e = new Axios(i)
          , t = bind(Axios.prototype.request, e);
        return utils.extend(t, Axios.prototype, e),
        utils.extend(t, e),
        t.create = function(n) {
            return createInstance(mergeConfig(i, n))
        }
        ,
        t
    },
    mapLimit(i, e, t) {
        return new Promise((r,n)=>{
            const o = i.length;
            let a = e - 1
              , s = 0;
            const l = u=>{
                u.forEach(c=>{
                    t(c).then(()=>{
                        if (s++,
                        s === o) {
                            r();
                            return
                        }
                        a++;
                        const h = i[a];
                        h && l([h])
                    }
                    , h=>{
                        n(h)
                    }
                    )
                }
                )
            }
            ;
            l(i.slice(0, e))
        }
        )
    },
    isWebAssemblySupported(){
        try {
            if (typeof WebAssembly == "object" && typeof WebAssembly.instantiate == "function") {
                const i = new WebAssembly.Module(Uint8Array.of(0, 97, 115, 109, 1, 0, 0, 0));
                if (i instanceof WebAssembly.Module)
                    return new WebAssembly.Instance(i)instanceof WebAssembly.Instance
            }
        } catch {}
        return console.log("wasm is not supported"),
        !1
    },
    isSupported() {
        return typeof RTCPeerConnection == "function" && this.isWebAssemblySupported()
    },
    objectParseFloat(i){
        const e = {};
        return i && Object.keys(i).forEach(t=>{
            e[t] = parseFloat(i[t])
        }
        ),
        e
    },
    getRandomItem(i){
        if(i.length === 0){
            return null
        }
        else{
            return i[Math.floor(Math.random() * i.length)]
        }
        //i.length === 0 ? null : i[Math.floor(Math.random() * i.length)]
    },
    getErrorByCode(i) {
        if (i === Codes.Success)
            return InternalError;
        const e = CodeErrorMap[i];
        return e || console.warn("unkown code", i),
        e || InternalError
    },
    getDistance(i, e) {
        const {x: t, y: r, z: n} = i
          , {x: o, y: a, z: s} = e;
        return Math.sqrt(Math.abs(t - o) ** 2 + Math.abs(r - a) ** 2 + Math.abs(n - s) ** 2)
    }
    , 
    //pitch，yaw，roll转rotation
    ue4Rotation2Xverse(i){
        if(this.isRotationCorrect(i)){
            if(i.pitch >= 89.5){
                i.pitch = 89.5
            }
            else if(i.pitch <= -89.5){
                i.pitch = -89.5
            }

            if(Math.PI * i.roll / 180 < .001){
                return new BABYLON.Vector3(-1 * Math.PI * i.pitch / 180,Math.PI * i.yaw / 180 - Math.PI * 27 / 18, 0 )
            }
            else{
                return new BABYLON.Vector3(-1 * Math.PI * i.pitch / 180,Math.PI * i.yaw / 180 - Math.PI * 27 / 18, Math.PI * i.roll / 180 )
            }
        }
        else{
            return null
        }
    },

    ue4Rotation2Xverse_mesh(i){
        if(this.isRotationCorrect(i)){
            if(Math.abs(Math.PI * i.roll) / 180 < .001){
                return new BABYLON.Vector3(Math.PI * i.pitch / 180,Math.PI * i.yaw / 180,0)
      
            }
            else{
                return new BABYLON.Vector3(Math.PI * i.pitch / 180,Math.PI * i.yaw / 180, -1 * (Math.PI * i.roll) / 180)
            }
            
        }
        else{
            return null;
        }
    },
    ue4Scaling2Xverse(i){
        if(this.isScalingCorrect(i)){
            return new BABYLON.Vector3(i.x,i.z,-1 * i.y)
        }
        else{
            return null;
        }
    },
    ue4Position2Xverse(i){
        if(this.isPositionCorrect(i)){
            return new BABYLON.Vector3(i.x * .01,i.z * .01,-1 * i.y * .01)
        }
        else{
            return null;
        }
    },
    //rotation转成pitch，yaw，roll
    xversePosition2Ue4(i){
        if(this.isPositionCorrect(i)){
            return {
                x: i.x * 100,
                y: -1 * i.z * 100,
                z: i.y * 100
            }
        }
        else{
            return null;
        }
    },
    xverseRotation2Ue4(i){
        if (this.isPositionCorrect(i)) {
            let e = 0;
            i.z == 0 ? e = 0 : e = 180 * i.z / Math.PI;
            return {
                pitch: 180 * i.x * -1 / Math.PI,
                yaw: (i.y + Math.PI * 27 / 18) * 180 / Math.PI,
                roll: e
            }
        } 
        else
        {
            return null
        }
    },
    //isRotationCorrect = i=>!0
    isRotationCorrect(i){
        if(i){
            return true
        }
        else{
            return false
        }
    },
    isPositionCorrect(i){
        if(i){
            return true
        }
        else{
            return false
        }
    },
    isScalingCorrect(i){
        if(i){
            return true
        }
        else{
            return false
        }
    }
}
export default util
