import { z } from 'zod';

export default class Network  {
    prefix = `NETWORK`
    indices = null

    constructor(indices) {
        this.indices = indices
    }

    schema = z.object({
        id: z.number(),
        name: z.string(),
        apps: z.array(z.string()),
        logo: z.string(),
        abbreviation: z.string()
    })
    .strict()

    get(id) {
        let index = this.indices[0] + (id - 1)
        return global.db.nodes[index]
    }

    save(n) {
        let id = `${this.prefix}-${n.id}`
        let result = this.schema.safeParse(n)
        if(result.success) {
            try {
                super.add(id, n)
            } catch(e) {
                console.error(e)
                throw e
            }
        } else {
            console.error(result.error)
            throw new global.ServerError(400, "Invalid Member Data!");
        }
    }

    add(n) {
        let toSave = {
            id: this.entries.length+1,
            ...n
        }
        this.save(toSave)
    }
}