import { ICellRendererParams } from "ag-grid-community";
import BaseRenderer from "./base-renderer";

export default class TagRenderer extends BaseRenderer {
    public init(params: ICellRendererParams) {
        super.init(params);
        super.setupGui();
    }

    public setupContentGui(): string {
        if (this.params.data == null || this.params.colDef?.field == null) {
            return '';
        }
        const fields = this.params.colDef.field.split('.');
        let label = this.params.data;
        for (let field of fields) {
            label = label[field];
        }
        return `<ui-tag label="${label}" text-color="${this.getColor("textColor", "#ffffff")}" background-color="${this.getColor("backgroundColor", "#6d7277")}" size="md" type="solid"></ui-tag>`;
    }


    private getColor(property: string, defaultColor: string) {
        let color: string;
        if (this.params.colDef.filterParams?.[property] == null) {
            color = defaultColor;
        } else {
            const colorParts = this.params.colDef.filterParams[property].split('.');
            color = this.params.data;
            for (let part of colorParts) {
                color = color[part];
            }
            if (color == null || typeof color != 'string') {
                color = this.params.colDef.filterParams[property];
            }
        }
        return color;
    }
}
