export function vectorize(value) {
    if (value === undefined) {
        console.warn('vectorizing undefined as null')
        return null
    }
    if (value === null)
        return null
    if (value.length === undefined)
        value = [value]
    return value;
}

export function devectorize(value) {
    return value === null || value.length === 0 ? null : value.length === 1 ? value[0] : value
}


export function vectorIsNull(value) {
    return value === null || value.length === 1 && value[0] === null
}


export function vectorIsZero(value) {
    for (let i = 0; i < value.length; i++)
        if (value[i] !== 0)
            return false
    return true
}


export function vectorEquals(a, b) {
    if (a === b) return true
    if (a === null && b !== null || a !== null && b === null) return false
    // noinspection JSObjectNullOrUndefined
    if (a.length !== b.length )
        return false
    for (let i = 0; i < a.length; i++)
        if (a[i] !== b[i])
            return false
    return true
}


export function vectorNeg(a) {
    return a.map((v)=>-v)
}


export function vectorAdd(a, b) {
    const result = []
    const scalarB = b.length === undefined
    for (let i = 0; i < a.length; i++)
        result.push(a[i] + (scalarB ? b : b[i]))
    return result
}

export function vectorSub(a, b) {
    const result = []
    const scalarB = b.length === undefined
    for (let i = 0; i < a.length; i++)
        result.push(a[i] - (scalarB ? b : b[i]))
    return result
}

export function vectorMul(a, b) {
    const result = []
    const scalarB = b.length === undefined
    for (let i = 0; i < a.length; i++)
        result.push(a[i] * (scalarB ? b : b[i]))
    return result
}

export function vectorDiv(a, b) {
    const result = []
    const scalarB = b.length === undefined
    for (let i = 0; i < a.length; i++)
        result.push(a[i] / (scalarB ? b : b[i]))
    return result
}


export function vectorInterpolate(a, b, zeroToOne) {
    const d = vectorSub(b, a)
    const offset = vectorMul(d, zeroToOne)
    return vectorAdd(a, offset)
}

