import { toTypeString } from '@vue/shared'

export const toRawType = (value: any) => toTypeString(value).slice(8, -1)

export const normalizeUnitToStyle: number = (unit: number | string) => {
    if (unit === 0) {
        return unit
    } else if (typeof unit === 'number') {
        return unit ? (unit <= 1 && unit >= 0 ? `${100 * unit}%` : `${unit}px`) : 0
    } else if (unit.includes('px')) {
        return normalizeUnitToStyle(Number.parseFloat(unit))
    } else if (unit.includes('%')) {
        return normalizeUnitToStyle(Number.parseFloat(unit) / 100)
    } else {
        return unit
    }
}

export const os = (function () {
    const ua = navigator.userAgent
    const isWindowsPhone = /(?:Windows Phone)/.test(ua)
    const isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone
    const isAndroid = /(?:Android)/.test(ua)
    const isFireFox = /(?:Firefox)/.test(ua)
    // const isChrome = /(?:Chrome|CriOS)/.test(ua);
    let isTablet = /(?:iPad|PlayBook)/.test(ua) || (isAndroid && !/(?:Mobile)/.test(ua)) || (isFireFox && /(?:Tablet)/.test(ua))
    const isPhone = /(?:iPhone)/.test(ua) && !isTablet
    const isPc = !isPhone && !isAndroid && !isSymbian

    if (isPc && navigator.maxTouchPoints > 1) {
        isTablet = true
    }
    return {
        isTablet,
        isPhone,
        isAndroid,
        isPc,
    }
})()
