import { generateUUID } from '@/utils/tools.js'
import { defineStore } from 'pinia'

export const useMeterStore = defineStore('meter', {
    state: () => ({
        udid: ''
    }),
    actions: {
        initUdid() { 
            var udid = window.localStorage.getItem('UDID')
            if (!udid) {
                udid = generateUUID(); 
                window.localStorage.setItem("UDID", udid);
            }
            this.setUdid(udid)
        },
        setUdid(udid) {
            this.udid = udid
        },
        getUdid() {
            return this.udid
        },
        getSudid() {
            var typedArray = new Uint8Array(this.udid.match(/[\da-f]{2}/gi).map(function (h) {
                return parseInt(h, 16)
            }));
            const sudid = btoa(String.fromCharCode.apply(null, typedArray)).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');

            return sudid
        }
    }

})
