import { assertClientRenderableContentType } from "./metadata.types.generated.js";
async function resolveClientRenderableContent(content) {
    const resolved = await content('object');
    return ({
        type: assertClientRenderableContentType(await resolved.type('string')),
        content: await resolved.content('string')
    });
}
export async function resolveSharedAnchorMetadataLegalExtension(metadata, options) {
    if (!metadata) {
        return ({});
    }
    const resolvedField = await metadata('object');
    if (!resolvedField) {
        return ({});
    }
    const disclaimers = await (async () => {
        const resolvedDisclaimers = await resolvedField.disclaimers?.('array');
        if (!resolvedDisclaimers) {
            return (undefined);
        }
        const parsedDisclaimers = await Promise.allSettled(resolvedDisclaimers.map(async function (disclaimer) {
            const resolvedDisclaimer = await disclaimer('object');
            const purpose = await resolvedDisclaimer.purpose('string');
            if (purpose !== 'general') {
                throw (new Error(`Unsupported disclaimer purpose: ${purpose}`));
            }
            return ({
                purpose: purpose,
                content: await resolveClientRenderableContent(resolvedDisclaimer.content)
            });
        }));
        const filtered = [];
        for (const result of parsedDisclaimers) {
            if (result.status === 'fulfilled') {
                filtered.push(result.value);
            }
            else {
                options.logger?.warn('resolveSharedAnchorMetadataLegalExtension', 'Failed to resolve disclaimer content', result.reason);
            }
        }
        return (filtered);
    })();
    return ({
        legal: {
            disclaimers
        }
    });
}
//# sourceMappingURL=metadata.types.js.map