# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/datasets/ac_types.ipynb.

# %% auto 0
__all__ = ['get_preferred_mains_ac_type', 'get_preferred_apps_ac_type', 'get_preferred_ac_type']

# %% ../../nbs/datasets/ac_types.ipynb 2
import nilmtk

import enilm.etypes
import enilm.datasets
import enilm.appliances
from ..yaml.config import ACTypes

# %% ../../nbs/datasets/ac_types.ipynb 3
def get_preferred_mains_ac_type(
    ds: enilm.etypes.DatasetID,
    house: enilm.etypes.HouseNr,
    physical_quantity="power",
    preferred_ac_types_sorted=["active", "apparent"],
) -> str:
    nilmtk_ds = enilm.datasets.get_nilmtk_dataset(ds)
    available_ac_types = (
        nilmtk_ds.buildings[house]
        .elec.mains()
        .available_ac_types(physical_quantity=physical_quantity)
    )

    for ac_type in preferred_ac_types_sorted:
        if ac_type in available_ac_types:
            return ac_type

    return available_ac_types[0]

# %% ../../nbs/datasets/ac_types.ipynb 5
def get_preferred_apps_ac_type(
    ds: enilm.etypes.DatasetID,
    house: enilm.etypes.HouseNr,
    app: enilm.etypes.AppName | nilmtk.Appliance,
    physical_quantity="power",
    preferred_ac_types_sorted=["active", "apparent"],
) -> str:
    nilmtk_ds = enilm.datasets.get_nilmtk_dataset(ds)
    app_elec = enilm.appliances.get_elec(app, nilmtk_ds.buildings[house].elec)
    available_ac_types = app_elec.available_ac_types(
        physical_quantity=physical_quantity
    )

    for ac_type in preferred_ac_types_sorted:
        if ac_type in available_ac_types:
            return ac_type

    return available_ac_types[0]

# %% ../../nbs/datasets/ac_types.ipynb 9
def get_preferred_ac_type(
    ds: enilm.etypes.DatasetID,
    house: enilm.etypes.HouseNr,
    app: enilm.etypes.AppName | nilmtk.Appliance,
    physical_quantity="power",
    preferred_ac_types_sorted=["active", "apparent"],
) -> ACTypes:
    return ACTypes(
        mains=get_preferred_mains_ac_type(
            ds, house, physical_quantity, preferred_ac_types_sorted
        ),
        apps=get_preferred_apps_ac_type(
            ds, house, app, physical_quantity, preferred_ac_types_sorted
        ),
    )
