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

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

    public setupContentGui(): string {
        let cellValue: string = this.getValueToDisplay();
        if(cellValue == null) {
            return '';
        }
        else {
            let enumerationType = this.getEnumerationType();
            return `<ui-tag label="${cellValue}" text-color="${enumerationType?.textColor}" background-color="${enumerationType?.backgroundColor}" size="md" type="solid"></ui-tag>`;
        }
    }

    private getEnumerationType(): EnumerationType {
        if(this.params.colDef.field) {
            let path = this.params.colDef.field.split(".denomination")[0].split(".");
            let enumerationType: any = this.params.data;
            if(path != null && Array.isArray(path)) {
                path.forEach(variable => {
                    enumerationType = enumerationType[variable];
                });
            }
            return enumerationType as EnumerationType;
        }
    }
 }