var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { logger } from '@enbox/common';
/**
 * Reads from the local DWN first, then falls through to the remote DWN when the
 * local reply is well-formed but missing the requested durable dependency.
 */
export function processDwnRequestWithRemoteFallback(executor, request, hasUsableReply) {
    return __awaiter(this, void 0, void 0, function* () {
        const localResponse = yield executor.process(request);
        if (hasUsableReply(localResponse.reply)) {
            return localResponse;
        }
        try {
            return yield executor.send(request);
        }
        catch (error) {
            logger.log(`AgentDwnApi: remote fallback for ${request.messageType} to '${request.target}' failed: ` +
                `${error instanceof Error ? error.message : String(error)}`);
            return localResponse;
        }
    });
}
//# sourceMappingURL=dwn-read-through.js.map