export class product_base {
    name;
    description;
    author;
    barcode;
    stock_count;

    constructor(target = null) {
        this.name = this._extract(target, "name");
        this.description = this._extract(target, "description");
        this.author = this._extract(target, "author");
        this.barcode = this._extract(target, "barcode");
        this.stock_count = this._extract(target, "stock_count");

        if (this.stock_count !== null) {
            try {
                this.stock_count = Number(this.stock_count);
            } catch {
                this.stock_count = 0;
            }
        }
    }

    get dump() {
        return {
            "name": new String(this.name),
            "description": new String(this.description),
            "author": new String(this.author),
            "barcode": new String(this.barcode),
            "stock_count": new String(this.stock_count)
        }
    }

    _extract(dict, name) {
        if (dict === null) {
            return null;
        }

        if (name in dict) {
            return dict[name];
        }

        return null;
    }

    get avairable() {
        return (this.stock_count > 0);
    }
}   
