import React from 'react';
import {
    MPosControllerConfig,
    APIResponse,
    CommandMode
} from "react-native-bixolon-bgatelib";

export async function SearchDevices(commandMode: CommandMode, interfaceType: number, address: string, setDeviceIdList: React.Dispatch<Array<number>>) {
    let config = new MPosControllerConfig();

    try {
        await config.selectCommandMode(commandMode);
        await config.selectInterface(interfaceType, address);

        let result = await config.openService(-1 /* fixed*/, 3 /* seconds*/);

        if (result == APIResponse.RES_SUCCESS) {
            const list = await config.searchDevices();
            setDeviceIdList(list);
        } else {
            console.error("Error: OpenService.  result: " + APIResponse[result]);
        }
    } catch (e) {
        console.error("Exception : " + e)
    } finally {
        await config.closeService(3);
    }
}
