import { Errors } from './common.js';
/* XXX: Move this somewhere more common */
export function MethodLogger(input, from) {
    if (input === undefined) {
        // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
        return undefined;
    }
    const fromStr = `${from.class}${from.instanceID ? `:${from.instanceID}` : ''}::${from.method}`;
    const retval = {
        debug: function (...logArgs) {
            input.debug(fromStr, ...logArgs);
        },
        info: function (...logArgs) {
            input.info(fromStr, ...logArgs);
        },
        warn: function (...logArgs) {
            input.warn(fromStr, ...logArgs);
        },
        error: function (...logArgs) {
            input.error(fromStr, ...logArgs);
        },
        log: function (...logArgs) {
            input.log(fromStr, ...logArgs);
        }
    };
    // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
    return retval;
}
export function ManageStatusUpdates(id, existingEntry, status, ancillary, logger) {
    const retval = {
        status: status,
        worker: ancillary?.by ?? null,
        updated: new Date()
    };
    const { oldStatus, output } = ancillary ?? {};
    if (oldStatus && existingEntry.status !== oldStatus) {
        throw (new Errors.IncorrectStateAssertedError(id, oldStatus, existingEntry.status));
    }
    logger?.debug(`Setting request with id ${String(id)} status from "${existingEntry.status}" to "${status}"`);
    if (status === 'failed_temporarily') {
        const existingFailures = existingEntry.failures;
        retval.failures = existingFailures + 1;
        logger?.debug(`Incrementing failure count for request with id ${String(id)} to ${retval.failures}`);
    }
    if (status === 'pending' || status === 'completed') {
        logger?.debug(`Clearing last error for request with id ${String(id)}`);
        retval.lastError = null;
    }
    if (ancillary?.error) {
        retval.lastError = ancillary.error;
        logger?.debug(`Setting last error for request with id ${String(id)} to:`, ancillary.error);
    }
    if (output !== undefined) {
        retval.output = output;
        logger?.debug(`Setting output for request with id ${String(id)} to:`, output);
    }
    return (retval);
}
export function ConvertStringToRequestID(input) {
    if (input === undefined) {
        return (undefined);
    }
    // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
    return input;
}
//# sourceMappingURL=internal.js.map