# -*- coding: utf-8 -*-
import json

import sysactions

from actions.util import get_all_tender_types


@sysactions.action
def get_selected_other_tender(
    pos_id,             # type: str
    tender_options,     # type: str
):
    # type: (...) -> str

    tender_types = get_all_tender_types()
    tender_options = tender_options.split(',')
    tender_types = [tender for tender in tender_types if str(tender["id"]) in tender_options]
    tender_options = [tender["descr"] for tender in tender_types]
    selected_option = sysactions.show_listbox(
        posid=pos_id,
        options=tender_options,
    )
    if selected_option is None:
        return "{}"

    selected_tender = tender_types[selected_option]

    return json.dumps({
        "id": selected_tender.get("id"),
        "electronicTypeId": selected_tender.get("electronicTypeId"),
    })
