# 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



log = logging.getLogger(__name__)

DCATAPIT_THEMES_MAP = 'ckanext.dcatapit.nonconformant_themes_mapping.file'
DCATAPIT_THEMES_MAP_SECTION = 'terms_theme_mapping'


def themes_to_aggr_json(themes: list) -> str:
    aggr = []
    for name in themes or []:
        name = name.split('/')[-1]  # if it's a URL, only take the final name
        aggr.append({'theme': name.upper(), 'subthemes': []})
    return json.dumps(aggr)


def theme_aggr_to_theme_uris(aggregated_themes: list) -> list:

# 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




log = logging.getLogger(__name__)

DCATAPIT_THEMES_MAP = 'ckanext.dcatapit.nonconformant_themes_mapping.file'
DCATAPIT_THEMES_MAP_SECTION = 'terms_theme_mapping'


def themes_to_aggr_json(themes: list) -> str:
    aggr = []
    for name in themes or []:
        name = name.split('/')[-1]  # if it's a URL, only take the final name
        aggr.append({'theme': name.upper(), 'subthemes': []})
    return json.dumps(aggr)


def theme_aggr_to_theme_uris(aggregated_themes: list) -> list:

    return [theme_name_to_uri(agg.get('theme')) for agg in aggregated_themes]