import type { tl } from '@mtcute/tl'

import type { ITelegramClient } from '../../client.types.js'
import type { InputPeerLike } from '../../types/index.js'
import { createDummyUpdate } from '../../updates/utils.js'
import { resolveChannel, resolvePeer } from '../users/resolve-peer.js'

/**
 * Delete all messages of a user (or channel) in a supergroup
 */
export async function deleteUserHistory(
    client: ITelegramClient,
    params: {
        /** Chat ID */
        chatId: InputPeerLike
        /** User/channel ID whose messages to delete */
        participantId: InputPeerLike

        /**
         * Whether to dispatch the updates that will be generated by this call.
         * Doesn't follow `disableNoDispatch`
         */
        shouldDispatch?: true
    },
): Promise<void> {
    const { chatId, participantId, shouldDispatch } = params

    const channel = await resolveChannel(client, chatId)

    const peer = await resolvePeer(client, participantId)

    const res = await client.call({
        _: 'channels.deleteParticipantHistory',
        channel,
        participant: peer,
    })

    if (!shouldDispatch) {
        client.handleClientUpdate(createDummyUpdate(res.pts, res.ptsCount, (channel as tl.RawInputChannel).channelId))
    }
}
