from .database import *


def get_title_schema(name: str) -> dict[str, dict[str, Any]]:
    return {'value': {'type': 'float', 'optional': False, 'readonly': False}}


def get_title(name: str) -> dict[str, Any]:
    title = read(name, 'select * from title')
    return { 'value': title['value'] }


def _set_title(name: str, cs: ChangeSet) -> DbChangeSet:
    new = cs.operations[0]['value']
    old = get_title(name)['value']

    redo_sql = f"update title set value = '{new}';"
    undo_sql = f"update title set value = '{old}';"

    redo_cs = g_update_prefix | { 'type': 'title', 'value': new }
    undo_cs = g_update_prefix | { 'type': 'title', 'value': old }

    return DbChangeSet(redo_sql, undo_sql, [redo_cs], [undo_cs])


def set_title(name: str, cs: ChangeSet) -> ChangeSet:
    return execute_command(name, _set_title(name ,cs))


def inp_in_title(section: list[str]) -> str:
    if section == []:
        return str('')

    title = '\n'.join(section)
    return str(f"update title set value = '{title}';")


def inp_out_title(name: str) -> list[str]:
    obj = str(get_title(name)['value'])
    return obj.split('\n')
