local ____lualib = require("lualib_bundle")
local __TS__Class = ____lualib.__TS__Class
local __TS__ClassExtends = ____lualib.__TS__ClassExtends
local __TS__ArrayFindIndex = ____lualib.__TS__ArrayFindIndex
local __TS__ArraySplice = ____lualib.__TS__ArraySplice
local ____exports = {}
local ____sort = require("functions.sort")
local sortObjectArrayByKey = ____sort.sortObjectArrayByKey
local stableSort = ____sort.stableSort
local ____Feature = require("classes.private.Feature")
local Feature = ____Feature.Feature
--- The base class for a custom callback. Individual custom callbacks (and validation callbacks) will
-- extend from this class.
____exports.CustomCallback = __TS__Class()
local CustomCallback = ____exports.CustomCallback
CustomCallback.name = "CustomCallback"
__TS__ClassExtends(CustomCallback, Feature)
function CustomCallback.prototype.____constructor(self, ...)
    Feature.prototype.____constructor(self, ...)
    self.subscriptions = {}
    self.fire = function(____, ...)
        local fireArgs = {...}
        for ____, subscription in ipairs(self.subscriptions) do
            local callbackFunc = subscription.callbackFunc
            local optionalArgs = subscription.optionalArgs
            if self:shouldFire(fireArgs, optionalArgs) then
                local value = callbackFunc(
                    nil,
                    fireArgs[1],
                    fireArgs[2],
                    fireArgs[3],
                    fireArgs[4],
                    fireArgs[5],
                    fireArgs[6],
                    fireArgs[7]
                )
                if value ~= nil then
                    return value
                end
            end
        end
        return nil
    end
    self.shouldFire = function() return true end
end
function CustomCallback.prototype.addSubscriber(self, priority, callbackFunc, ...)
    local optionalArgs = {...}
    local subscription = {priority = priority, callbackFunc = callbackFunc, optionalArgs = optionalArgs}
    local ____self_subscriptions_0 = self.subscriptions
    ____self_subscriptions_0[#____self_subscriptions_0 + 1] = subscription
    self.subscriptions = stableSort(
        nil,
        self.subscriptions,
        sortObjectArrayByKey(nil, "priority")
    )
end
function CustomCallback.prototype.removeSubscriber(self, callback)
    local subscriptionIndexMatchingCallback = __TS__ArrayFindIndex(
        self.subscriptions,
        function(____, subscription)
            local subscriptionCallback = subscription.callbackFunc
            return callback == subscriptionCallback
        end
    )
    if subscriptionIndexMatchingCallback ~= -1 then
        __TS__ArraySplice(self.subscriptions, subscriptionIndexMatchingCallback, 1)
    end
end
return ____exports
