import Taro from "@tarojs/taro";



export default function showActionSheet(props: any) {
    if (process.env.TARO_ENV == 'weapp') {
        Taro.showActionSheet({
            alertText:props.title??'',
            itemList: props.itemList,
            success: function (res) {
                props.success(res.tapIndex)
            },
            fail: function (res) {
                if (props.fail)
                    props.fail(res)
            }
        })
    }
    else {
        const { showActionSheetWithOptions } = props.showActionSheetWithOptions;//useActionSheet();
        var list = JSON.parse(JSON.stringify(props.itemList));
        list.push('Cancel');
        let options = {
            options: list,
            cancelButtonIndex: list.length - 1,
            // destructiveButtonIndex: 0,
            title: props.title,
            message: props.message,
        };
        var destructiveButtonIndex = -1;
        list.map((item, index) => {
            if (item == 'Delete' || item == '删除'|| item == 'Delete Account'|| item == '注销账号')
                destructiveButtonIndex = index
        });
        (options as any).destructiveButtonIndex = destructiveButtonIndex
        showActionSheetWithOptions(options, (selectedIndex: number) => {
            if (selectedIndex < list.length - 1)
                props.success(selectedIndex)
        });
    }
}