import { UIMenuItem, Point, Menu } from "./libs/NativeUI/index";

function createMenuItem(title: string, description?: string, fn?: (menuItem: UIMenuItem) => void): UIMenuItem {
    var item: UIMenuItem = new UIMenuItem(title, description || "");
    if (fn) {
        fn(item);
    }

    return item;
}

function createMenu(title: string, subtitle: string, pos: Point, items: MenuMap): Menu {
    var menu: Menu = new Menu(title, subtitle, pos);

    Object.entries(items).forEach((value, index, array) => {
        menu.AddItem(items[value[0]]);
    });

    menu.Close(true);

    return menu;
}

function parseJson<TData>(json: string): TData {
    var dateTimeReviver = function (key: string, value: any) {
        var a;
        if (typeof value === 'string') {
            a = /\/Date\((\d*)\)\//.exec(value);
            if (a) {
                return new Date(+a[1]);
            }
        }
        return value;
    }

    return <TData>JSON.parse(json, dateTimeReviver);
}

interface MenuMap {
    [key: string]: UIMenuItem;
}

export {
    createMenuItem,
    parseJson,
    createMenu,
    MenuMap
}