import { cfg } from "./Config";
import { Database } from "./LocalDatabase";

export class DataHandler {
    private db: &Database;

    constructor(db: &Database) {
        this.db = db;
    }

    valid_uri(uri: string) {
        let found: boolean = false;
        for(const key of Object.keys(cfg)) {
            if(cfg[key].uri == uri) {
                found = true;
                break;
            }
        }
        return found;
    }

    get_data_json(uri: string): string {
        let found: boolean = false;
        let data: string = '{';
        let dp;
        for(const key of Object.keys(cfg)) {
            if(cfg[key].uri == uri) {
                found = true;
                dp = cfg[key];
                break;
            }
        }

        let add_comma: boolean = false;
        let o = Object.keys(dp);
        o.forEach(d => {
            let s = this.db.get(dp[d]);
            if(s != null) {
                if(add_comma) {
                    data += ","
                }
                data += '"' + d + '":"' + s + '"';
                add_comma = true;
            }
        });

        data += '}';

        return data;
    }
}