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 { DwnInterfaceName, DwnMethodName, Message } from '@enbox/dwn-sdk-js';
/**
 * Helpers shared by the push (`sync-messages.ts`) and pull (`sync-admit-closure.ts`)
 * dependency-closure fetch paths. These were previously duplicated verbatim in both
 * modules; keep them here so the two paths cannot drift.
 */
/**
 * Resolves the delegate permission grant ID authorizing a sync fetch request, or
 * `undefined` for owner requests (no delegate configured) and when no matching grant
 * exists. Only the expected "no matching grant" case is swallowed; any other error
 * (store/network failure, revocation-check failure, parse error) surfaces so a real
 * resolution failure is not silently treated as "no grant".
 */
export function resolveDelegatePermissionGrantId(deps, messageType, protocol) {
    return __awaiter(this, void 0, void 0, function* () {
        if (deps.delegateDid === undefined || deps.permissionsApi === undefined) {
            return undefined;
        }
        try {
            const { grant } = yield deps.permissionsApi.getPermissionForRequest({
                connectedDid: deps.did,
                delegateDid: deps.delegateDid,
                protocol,
                cached: true,
                messageType,
            });
            return grant.id;
        }
        catch (error) {
            if (error instanceof Error && error.message.includes('No permissions found')) {
                return undefined;
            }
            throw error;
        }
    });
}
/** Stable cache/identity key for a dependency reference. */
export function dependencyKey(ref) {
    return JSON.stringify(ref);
}
/** Whether any dependency is known to be unreachable (a terminal closure failure). */
export function hasTerminalDependency(refs) {
    return refs.some(ref => ref.terminal === true);
}
/** Human-readable detail listing the missing dependencies. */
export function missingDependencyDetail(refs) {
    return refs.map(dependencyKey).join(', ');
}
/** Type guard for a `ProtocolsConfigure` message. */
export function isProtocolsConfigureMessage(message) {
    return message.descriptor.interface === DwnInterfaceName.Protocols &&
        message.descriptor.method === DwnMethodName.Configure &&
        message.descriptor.definition !== undefined;
}
/** Predicate matching a tenant's own configuration of a given protocol. */
export function isTenantProtocolConfig(tenantDid, protocol) {
    return (message) => {
        if (!isProtocolsConfigureMessage(message)) {
            return false;
        }
        return message.descriptor.definition.protocol === protocol && Message.getAuthor(message) === tenantDid;
    };
}
/** Returns the newest protocol configuration by message timestamp. */
export function newestProtocolConfig(configs) {
    let newest;
    for (const config of configs) {
        if (newest === undefined || config.descriptor.messageTimestamp > newest.descriptor.messageTimestamp) {
            newest = config;
        }
    }
    return newest;
}
//# sourceMappingURL=sync-fetch-helpers.js.map