/* eslint-disable unicorn/filename-case */
/* eslint-disable perfectionist/sort-objects */
import log from './log.js';
/* eslint-disable no-await-in-loop */
// Same at `setTimeout`, but `setTimeout` is only available in browsers
// It is also, non-blocking allowing other asynchronous events to be processed
// in the event loop.
const sleep = milliseconds => new Promise(resolve => {
    const start = Date.now();
    const wait = () => {
        if (Date.now() - start >= milliseconds) {
            resolve();
        }
        else {
            setImmediate(wait);
        }
    };
    wait();
});
// Display loading circle until 
const asyncWaitForState = async ({ startMsg, successMsg, desiredState, describeFn, describeArgs, resCallback, }) => {
    const spinner = log.spin(startMsg);
    let resourceState = '';
    while (resourceState !== desiredState) {
        await sleep(500);
        const response = await describeFn(describeArgs);
        resourceState = resCallback(response);
    }
    spinner.succeed(log.success(successMsg));
};
export default asyncWaitForState;
