import { assertNever } from '../../lib/utils/never.js';
import { KeetaAnchorCertificateRequiredError, KeetaAnchorUserError } from '../../lib/error.js';
import { assertNotificationChannelType, assertNotificationSubscriptionType } from './common.generated.js';
import { KeetaNet } from '../../client/index.js';
export * from './common.generated.js';
const notificationNamespace = '75ed4792-333f-4a50-832f-b9fcc5663d1f';
export function getNotificationRegisterTargetRequestSignable(request) {
    const parts = [
        notificationNamespace,
        'REGISTER_TARGET',
        request.channel.type
    ];
    if (request.channel.type === 'FCM') {
        parts.push(request.channel.fcmToken, request.channel.appId);
    }
    else {
        assertNever(request.channel.type);
    }
    return (parts);
}
export function getNotificationListTargetsRequestSignable(_ignore_request) {
    return ([
        notificationNamespace,
        'LIST_TARGETS'
    ]);
}
export function getNotificationDeleteTargetRequestSignable(request) {
    return ([
        notificationNamespace,
        'DELETE_TARGET',
        request.id
    ]);
}
export function getNotificationCreateSubscriptionRequestSignable(request) {
    const parts = [
        notificationNamespace,
        'CREATE_SUBSCRIPTION',
        request.subscription.type
    ];
    parts.push(request.subscription.locale?.toString() ?? 'NO_LOCALE');
    if (request.subscription.type === 'RECEIVE_FUNDS') {
        parts.push(request.subscription.toAddress ?? 'NO_ADDRESS');
    }
    else {
        assertNever(request.subscription.type);
    }
    parts.push(request.subscription.target.provider ?? 'NO_PROVIDER');
    parts.push('BEGIN_TARGETS');
    for (const target of request.subscription.target.ids) {
        parts.push(target);
    }
    parts.push('END_TARGETS');
    return (parts);
}
export function getNotificationDeleteSubscriptionRequestSignable(request) {
    return ([
        notificationNamespace,
        'DELETE_SUBSCRIPTION',
        request.id
    ]);
}
export function parseSubscriptionDetailsWithID(input) {
    let subscription;
    if (input.subscription.type === 'RECEIVE_FUNDS') {
        let locale;
        if (input.subscription.locale) {
            if (typeof input.subscription.locale === 'string') {
                locale = new Intl.Locale(input.subscription.locale);
            }
            else {
                locale = input.subscription.locale;
            }
        }
        subscription = {
            type: 'RECEIVE_FUNDS',
            target: input.subscription.target,
            ...(locale ? { locale } : {}),
            ...(input.subscription.toAddress
                ? { toAddress: KeetaNet.lib.Account.toAccount(input.subscription.toAddress) }
                : {})
        };
    }
    else {
        assertNever(input.subscription.type);
    }
    return ({ id: input.id, subscription });
}
export function getNotificationListSubscriptionsRequestSignable(_ignore_request) {
    return ([
        notificationNamespace,
        'LIST_SUBSCRIPTIONS'
    ]);
}
class KeetaNotificationAnchorMethodNotSupportedError extends KeetaAnchorUserError {
    static name = 'KeetaNotificationAnchorMethodNotSupportedError';
    KeetaNotificationAnchorMethodNotSupportedErrorObjectTypeID;
    static KeetaNotificationAnchorMethodNotSupportedErrorObjectTypeID = '95268e45-2305-4a75-92af-417feb57fdd6';
    logLevel = 'INFO';
    channelType;
    subscriptionType;
    constructor(properties, message) {
        super(message ?? `Request failed: ${properties.channelType ? `channel type ${properties.channelType}` : `subscription type ${properties.subscriptionType}`} not supported`);
        this.statusCode = 400;
        Object.defineProperty(this, 'KeetaNotificationAnchorMethodNotSupportedErrorObjectTypeID', {
            value: KeetaNotificationAnchorMethodNotSupportedError.KeetaNotificationAnchorMethodNotSupportedErrorObjectTypeID,
            enumerable: false
        });
        this.channelType = properties.channelType;
        this.subscriptionType = properties.subscriptionType;
    }
    static isInstance(input) {
        return (this.hasPropWithValue(input, 'KeetaNotificationAnchorMethodNotSupportedErrorObjectTypeID', KeetaNotificationAnchorMethodNotSupportedError.KeetaNotificationAnchorMethodNotSupportedErrorObjectTypeID));
    }
    toJSON() {
        return ({
            ...super.toJSON(),
            channelType: this.channelType,
            subscriptionType: this.subscriptionType
        });
    }
    static async fromJSON(input) {
        const { message, other } = this.extractErrorProperties(input, this);
        let subscriptionType;
        if ('intent' in other && other.intent !== undefined) {
            subscriptionType = assertNotificationSubscriptionType(other.subscriptionType);
        }
        let channelType;
        if ('channelType' in other && other.channelType !== undefined) {
            channelType = assertNotificationChannelType(other.channelType);
        }
        const error = new this({ subscriptionType, channelType }, message);
        error.restoreFromJSON(other);
        return (error);
    }
}
export const Errors = {
    MethodNotSupported: KeetaNotificationAnchorMethodNotSupportedError,
    CertificateRequired: KeetaAnchorCertificateRequiredError
};
//# sourceMappingURL=common.js.map