import { Calculator, Drug, Input, Interpretation, Values, inputOptions } from "../types"
import { DrugRegistry } from "./drugs/drug_registry"

function generateDrugList():inputOptions[] {
    const drugs:inputOptions[] = DrugRegistry.map(e => {
        const drug:inputOptions = {
            label:e.name,
            value:e.id
        }
        return drug;
    });
    return drugs;
}
const parameters:Input[] = [
    {
        id:"drug",
        name:"Drug name",
        placeholder:"Select the drug by searching its name below",
        type:"search",
        inputOptions:generateDrugList()
    },
    {
        id:"weight",
        name:"Weight",
        placeholder:"Enter the weight of the patient",
        type:"number"
    },
    {
        id:"ckd",
        name:"CKD Stage",
        placeholder:"Whether the patient has CKD, if yes, select the stage",
        type:"select",
        inputOptions:[
            {
                label:"None",
                value:"none"
            },
            {
                label:"CKD stage 3a",
                value:"3A"
            },
            {
                label:"CKD Stage 3B",
                value:"3B"
            },
            {
                label:"CKD Stage 4",
                value:"4"
            },
            {
                label:"CKD Stage 5 / ESRD",
                value:"5"
            }
        ]
    },
    {
        id:"cld",
        name:"CLD Stage (Child Pugh)",
        placeholder:"Whether the patient has CLD, if yes, select the Child Pugh Stage",
        type:"select",
        inputOptions:[
            {
                label:"None",
                value:"none"
            },
            {
                label:"Child Pugh A",
                value:"A"
            },
            {
                label:"Child Pugh B",
                value:"B"
            },
            {
                label:"Child Pugh C",
                value:"C"
            }
        ]
    }
]

export const Adult_Drug_Calculator:Calculator = {
    id:"adult_drug_calculator",
    name:"Adult Dose Calculator",
    desc:"Adult Dose Calculator can be used to calculate the drug dosing of different dose which also accounts for different comorbidities like CKD and CLD",
    inputs:parameters,
    calc_func:(values : Values) : string | number => {
        const drugID = values.drug as string;
        const weight = values.weight as number;
        const ckd = values.ckd as "none" | "3A" | "3B" | "4" | "5";
        const cld = values.cld as "none" | "A" | "B" | "C";
        const drug = DrugRegistry.find(e => e.id === drugID);
        if(!drug) return -1;
        const dosingObj_CLD = drug.cld_dosing?.find(e => e.stage === cld);
        const dosingObj_CKD = drug.ckd_dosing?.find(e => e.stage === ckd);
        if(drug){
            if(dosingObj_CLD && dosingObj_CKD && ckd !== "none" && cld !== "none"){
                if(dosingObj_CLD.dosing === 0 || dosingObj_CKD.dosing === 0){
                        return 0;
                    }
                if(drug.isWeightBased && typeof dosingObj_CLD.dosing === "number"){
                    return `${Math.floor(dosingObj_CLD.dosing * weight)} ${drug.unit}`
                }else{
                    return `${dosingObj_CLD.dosing} ${drug.unit}`
                }
            }else if(dosingObj_CLD && cld !== "none"){
                if(dosingObj_CLD.dosing === 0){
                    return 0;
                }
                if(drug.isWeightBased && typeof dosingObj_CLD.dosing === "number"){
                    
                    return `${Math.floor(dosingObj_CLD.dosing * weight)} ${drug.unit}`
                }else{
                    return `${dosingObj_CLD.dosing} ${drug.unit}`
                }
            }else if(dosingObj_CKD && ckd !== "none"){
                if(dosingObj_CKD.dosing === 0){
                    return 0;
                }
                if(drug.isWeightBased && typeof dosingObj_CKD.dosing === "number"){
                    
                    return `${Math.floor(dosingObj_CKD.dosing * weight)} ${drug.unit}`
                }else{
                    return `${dosingObj_CKD.dosing} ${drug.unit}`
                }
            }else{
                if(drug.isWeightBased && typeof drug.adult_dosing === "number"){
                    return `${Math.floor(drug.adult_dosing * weight)} ${drug.unit}`
                }else{
                    return `${drug.adult_dosing} ${drug.unit}`
                }
            }
        }else{
            return -1;
        }
    },
    interpret_func:(dosing:number | string):Interpretation => {
        if(dosing === 0){
            return {
                level:"severe",
                message:"Drug is to be avoided in patient"
            }
        }else if(dosing === -1){
            return {
                level:"none",
                message:"Drug not found"
            }
        }else{
            return {
                level:"low",
                message:`Calculated dosing for the drug is ${dosing ? dosing : null}`
            }
        }
    }
}