local m=require(script.Parent.Parent.TrueSignal)local q=require(script.Parent.prepArgs)local x={}x.__index=x function x._new(q,L)local E=setmetatable({},x)E.name=q E._server=L E.OnServerEvent=m.new(false,true)E._server._remoteEvents[q]=E return E end function x._fireServer(q,m,...)q.OnServerEvent:fire(m,...)end function x.destroy(m)m._server._remoteEvents[m.name]=nil for q,x in pairs(m._server._clients)do(x:getRemoteEvent(m.name)):_destroy()end m.OnServerEvent:destroy()m.destroyed=true end function x.fireClient(x,m,...)local L={q(...)}if m._remoteEvents[x.name]then(m:getRemoteEvent(x.name)):_fireClient(table.unpack(L))end end function x.fireAllClients(m,...)local x={q(...)}for q,L in pairs(m._server._clients)do if L._remoteEvents[m.name]then(L:getRemoteEvent(m.name)):_fireClient(table.unpack(x))end end end x.FireClient=x.fireClient x.FireAllClients=x.fireAllClients x.Destroy=x.destroy return x
local TrueSignal = require(script.Parent.Parent.TrueSignal)

local prepArgs = require(script.Parent.prepArgs)

--[=[
    RemoteEventServer class

    @class RemoteEventServer
]=]
local RemoteEventServer = {}
RemoteEventServer.__index = RemoteEventServer

--[=[
    Constructs a new RemoteEventServer object

    @param name string
    @param server Server
    @return RemoteEventServer

    @private
]=]
function RemoteEventServer._new(name, server)
    local self = setmetatable({}, RemoteEventServer)

    self.name = name
    self._server = server

    self.OnServerEvent = TrueSignal.new(false, true)
    
    self._server._remoteEvents[name] = self

    return self
end

--[=[
    Called by RemoteEventClient to fire the server

    @param client Client
    @param ... any
    
    @private
]=]
function RemoteEventServer:_fireServer(client, ...)
    self.OnServerEvent:fire(client, ...)
end

--[=[
    Prepares RemoteEventServer for garbage  collection
]=]
function RemoteEventServer:destroy()
    self._server._remoteEvents[self.name] = nil
    
    for _, client in pairs(self._server._clients) do
        client:getRemoteEvent(self.name):_destroy()
    end
    
    self.OnServerEvent:destroy()
    self.destroyed = true
end

--[=[
    Fires the corresponding RemoteEventClient instance's OnClientEvent signal with the passed arguments

    @param client Client
    @param ... any
]=]
function RemoteEventServer:fireClient(client, ...)
    local args = {prepArgs(...)}

    if client._remoteEvents[self.name] then
        client:getRemoteEvent(self.name):_fireClient(table.unpack(args))
    end
end

--[=[
    Fires the corresponding RemoteEventClient instance's OnClientEvent signal with the passed arguments for each client

    @param ... any
]=]
function RemoteEventServer:fireAllClients(...)
    local args = {prepArgs(...)}

    for _, client in pairs(self._server._clients) do
        if client._remoteEvents[self.name] then
            client:getRemoteEvent(self.name):_fireClient(table.unpack(args))
        end
    end
end

--[=[
    PascalCase alias for fireServer

    @param client Client
    @param ... any

    @method FireClient
    @within RemoteEventServer
]=]
RemoteEventServer.FireClient = RemoteEventServer.fireClient

--[=[
    PascalCase alias for fireAllClients

    @param ... any

    @method FireAllClients
    @within RemoteEventServer
]=]
RemoteEventServer.FireAllClients = RemoteEventServer.fireAllClients

--[=[
    PascalCase alias for destroy

    @method Destroy
    @within RemoteEventServer
]=]
RemoteEventServer.Destroy = RemoteEventServer.destroy

return RemoteEventServer