--[[
    Copyright 2020 Matthew Hesketh <matthew@matthewhesketh.com>
    This code is licensed under the MIT. See LICENSE for details.
]]

local delete = {}
local mattata = require('mattata')

function delete:init()
    delete.commands = mattata.commands(self.info.username):command('delete').table
    delete.help = '/delete [message ID] - Deletes the specified (or replied-to) message.'
end

function delete:on_message(message, configuration, language)
    if message.chat.type ~= 'supergroup'
    then
        return mattata.send_reply(
            message,
            language['errors']['supergroup']
        )
    elseif not mattata.is_group_admin(
        message.chat.id,
        message.from.id
    )
    then
        return mattata.send_reply(
            message,
            language['errors']['admin']
        )
    end
    local input = mattata.input(message.text)
    or (
        message.reply
        and message.reply.message_id
    )
    if not input
    or tonumber(input) == nil
    then
        return mattata.send_reply(
            message,
            delete.help
        )
    end
    local success = mattata.delete_message(
        message.chat.id,
        tonumber(input)
    )
    if not success
    then
        return mattata.send_reply(
            message,
            language['delete']['1']
        )
    end
    return mattata.delete_message(
        message.chat.id,
        message.message_id
    )
end

return delete