const urlService = require('../../core/server/services/url');

module.exports.isFinished = async () => {
    let timeout;

    return new Promise(function (resolve) {
        (function retry() {
            clearTimeout(timeout);

            if (urlService.facade.hasFinished()) {
                return resolve();
            }

            timeout = setTimeout(retry, 50);
        })();
    });
};

// Wait for a standalone (non-singleton) eager UrlService instance to finish
// its boot walk. Used by the parity integration tests, which construct their
// own instances instead of using the singleton `isFinished` above.
module.exports.waitUntilFinished = (standaloneUrlService, timeout = 5000) => {
    return new Promise((resolve, reject) => {
        const start = Date.now();
        (function retry() {
            if (standaloneUrlService.hasFinished()) {
                return resolve();
            }
            if (Date.now() - start > timeout) {
                return reject(new Error('Eager UrlService did not finish in time'));
            }
            setTimeout(retry, 50);
        })();
    });
};

// @TODO: unify all the reset/softTeset helpers so they either work how the main code works or the reasons why they are different are clear
module.exports.init = ({urlCache} = {}) => {
    urlService.init({urlCache});
};

module.exports.reset = () => {
    urlService.softReset();
},

module.exports.resetGenerators = () => {
    urlService.resetGenerators();
    urlService.resources.reset();
};
