# Path: ckanext/dcatapit/dcat/const.py
# THEME_BASE_URI = 'http://publications.europa.eu/resource/authority/data-theme/'
# 
# Path: ckanext/dcatapit/schema.py
# FIELD_THEMES_AGGREGATE = 'themes_aggregate'


import json
import logging
import os
from configparser import SafeConfigParser as ConfigParser
from ckan.lib.base import config
from ckan.model import Session
from ckan.model.group import Group, Member
from ckan.plugins import toolkit
from ckanext.dcatapit.dcat.const import THEME_BASE_URI
from ckanext.dcatapit.schema import FIELD_THEMES_AGGREGATE


def theme_aggrs_unpack(aggrs: list) -> (list, list):
    themes = []
    sub = []
    for aggr in aggrs:
        themes.append(theme_name_to_uri(aggr['theme']))
        sub.extend(aggr['subthemes'])

    return themes, sub


def themes_parse_to_uris(raw) -> list:
    if not raw:
        return []

    log.debug(f'Parsing themes field: {raw}')
    try:
        themes = json.loads(raw)
        themes_list = theme_names_to_uris(themes)
    except (TypeError, ValueError) as e:
        log.warning(f'Error parsing themes: {e} -- {raw}')
        if isinstance(raw, (list, tuple,)):
            log.debug(f'using themes list: {raw}')
            themes_list = theme_names_to_uris(raw)
        elif isinstance(raw, str):
            log.warning(f'Trying parsing old format: {raw}')
            themes_list = theme_names_to_uris(
                [name for name in raw.strip('{}').split(',')])
        else:

# Path: ckanext/dcatapit/dcat/const.py
# THEME_BASE_URI = 'http://publications.europa.eu/resource/authority/data-theme/'
# 
# Path: ckanext/dcatapit/schema.py
# FIELD_THEMES_AGGREGATE = 'themes_aggregate'


# Path: ckanext/dcatapit/mapping.py
import json
import logging
import os
from configparser import SafeConfigParser as ConfigParser
from ckan.lib.base import config
from ckan.model import Session
from ckan.model.group import Group, Member
from ckan.plugins import toolkit
from ckanext.dcatapit.dcat.const import THEME_BASE_URI
from ckanext.dcatapit.schema import FIELD_THEMES_AGGREGATE



def theme_aggrs_unpack(aggrs: list) -> (list, list):
    themes = []
    sub = []
    for aggr in aggrs:
        themes.append(theme_name_to_uri(aggr['theme']))
        sub.extend(aggr['subthemes'])

    return themes, sub


def themes_parse_to_uris(raw) -> list:
    if not raw:
        return []

    log.debug(f'Parsing themes field: {raw}')
    try:
        themes = json.loads(raw)
        themes_list = theme_names_to_uris(themes)
    except (TypeError, ValueError) as e:
        log.warning(f'Error parsing themes: {e} -- {raw}')
        if isinstance(raw, (list, tuple,)):
            log.debug(f'using themes list: {raw}')
            themes_list = theme_names_to_uris(raw)
        elif isinstance(raw, str):
            log.warning(f'Trying parsing old format: {raw}')
            themes_list = theme_names_to_uris(
                [name for name in raw.strip('{}').split(',')])
        else:

            log.error('No valid themes found')