import CfExceptionHandler from "../../../core/CfExceptionHandler";
import {checkObjectFieldEmptyiness} from "../../../utils";
import {injectCatalog} from "../../../core/injector";
import {DrugCatalog, FacilityCatalog, GroceryCatalog, ItemType} from "../typings";
import {BloodProperties} from "../blood.typings";
import {OxygenProperties} from "../oxygen.typings";
import {MedicalEquipmentProperties} from "../medicalEquipment.typings";

export async function validateAndSaveDrugCatalog(drugId: string, properties: DrugCatalog) {

    if (drugId == "") {
        new CfExceptionHandler().throwAndLogError(
            "drug catalog",
            "invalid drug id"
        );
        return;
    }
    if (Object.keys(properties).length === 0) {
        // it is allowed to the developer to pass an empty object,
        // but it will not be ingested
        return;
    }
    checkObjectFieldEmptyiness(properties, [
        "name",
        "market_id",
        "supplier_id",
        "supplier_name",
    ]);
    if (
        Object.keys(properties).length !== 0 &&
        properties.name.trim().length === 0
    ) {
        new CfExceptionHandler().throwAndLogError(
            "drug catalog",
            "drug name is required"
        );
    }
    if ((properties as DrugCatalog).active_ingredients) {
        (properties as any).active_ingredients =
            properties.active_ingredients.filter((item) => item.trim());
    }
    injectCatalog(drugId, ItemType.Drug, properties);
}

export async function validateAndSaveGroceryCatalog(groceryId: string, properties: GroceryCatalog) {

    if (Object.keys(properties).length === 0) {
        // it is allowed to the developer to pass an empty object,
        // but it will not be ingested
        return;
    }
    if (groceryId == "") {
        new CfExceptionHandler().throwAndLogError(
            "grocery catalog",
            "invalid grocery item id"
        );
    }
    checkObjectFieldEmptyiness(properties, ["name"]);
    if (
        Object.keys(properties).length !== 0 &&
        properties.name.trim().length === 0
    ) {
        new CfExceptionHandler().throwAndLogError(
            "grocery catalog",
            "grocery item name is required"
        );
    }
    if ((properties as GroceryCatalog).active_ingredients) {
        (properties as any).active_ingredients =
            properties.active_ingredients?.filter((item) => item.trim());
    }
    injectCatalog(groceryId, ItemType.Grocery, properties);
}

export async function validateAndSaveBloodCatalog(bloodId: string, properties: BloodProperties) {

    if (bloodId == "") {
        new CfExceptionHandler().throwAndLogError(
            "blood catalog",
            "invalid blood item id"
        );
    }
    if (Object.keys(properties).length === 0) {
        // it is allowed to the developer to pass an empty object,
        // but it will not be ingested
        return;
    }
    checkObjectFieldEmptyiness(properties, [
        "blood_component",
        "blood_group",
        "packaging",
        "packaging_units",
    ]);
    injectCatalog(bloodId, ItemType.Blood, properties);
}

export async function validateAndSaveOxygenCatalog(oxygenId: string, properties: OxygenProperties) {

    if (oxygenId == "") {
        new CfExceptionHandler().throwAndLogError(
            "oxygen catalog",
            "invalid oxygen item id"
        );
    }
    if (Object.keys(properties).length === 0) {
        // it is allowed to the developer to pass an empty object,
        // but it will not be ingested
        return;
    }
    checkObjectFieldEmptyiness(properties, [
        "market_id",
        "packaging",
        "packaging_units",
    ]);
    injectCatalog(oxygenId, ItemType.Oxygen, properties);
}

export async function validateAndSaveMedicalEquipmentCatalog(itemId: string, properties: MedicalEquipmentProperties) {

    if (itemId == "") {
        new CfExceptionHandler().throwAndLogError(
            "medical equipment catalog",
            "invalid medical equipment item id"
        );
    }
    if (Object.keys(properties).length === 0) {
        // it is allowed to the developer to pass an empty object,
        // but it will not be ingested
        return;
    }
    checkObjectFieldEmptyiness(properties, [
        "market_id",
        "name",
        "supplier_id",
        "supplier_name",
        "packaging",
        "packaging_units",
    ]);
    injectCatalog(itemId, ItemType.MedicalEquipment, properties);
}

export async function validateAndSaveFacilityCatalog(facilityId: string, properties: FacilityCatalog) {

    if (Object.keys(properties).length === 0) {
        // it is allowed to the developer to pass an empty object,
        // but it will not be ingested
        return;
    }
    if (facilityId == "") {
        new CfExceptionHandler().throwAndLogError(
            "facility catalog",
            "invalid facility id"
        );
    }
    checkObjectFieldEmptyiness(properties, ["name"]);
    if (
        Object.keys(properties).length !== 0 &&
        properties.name.trim().length === 0
    ) {
        new CfExceptionHandler().throwAndLogError(
            "facility catalog",
            "facility name is required"
        );
    }
    injectCatalog(facilityId, ItemType.Facility, properties);
}
