local Q=require(script.Parent.Parent.TrelloHttp)local M=require(script.Parent.Parent.Commons)local J={fields={"\105\100";"\110\097\109\101";"\099\108\111\115\101\100","\112\111\115"}}local w={RemoteId="\105\100";Name="\110\097\109\101",Archived="\099\108\111\115\101\100";Position="\112\111\115"}local v=M.generateMeta("\084\114\101\108\108\111\076\105\115\116")local a local f={}function f.new(M,J,w)if not w or getmetatable(w)~="\084\114\101\108\108\111\066\111\097\114\100"then error("\091\084\114\101\108\108\111\076\105\115\116\046\110\101\119\093\058 \073\110\118\097\108\105\100 \098\111\097\114\100\033",0)elseif not M or M==""or M:len()>16384 then error("\091\084\114\101\108\108\111\076\105\115\116\046\110\101\119\093\058 \073\110\118\097\108\105\100 \110\097\109\101\033 \077\097\107\101 \115\117\114\101 \116\104\097\116 \116\104\101 \110\097\109\101 \105\115 \097 \110\111\110-\101\109\112\116\121 \115\116\114\105\110\103 \119\105\116\104 \108\101\115\115 \116\104\097\110 \049\054\051\056\052 \099\104\097\114\097\099\116\101\114\115\046",0)end local v=w.Client:MakeURL("\047\108\105\115\116\115",{name=M;pos=J;idBoard=w.RemoteId})local f=Q.RequestInsist(v,Q.HttpMethod.POST,"\123\125",true)local z=a(f.Body)w._pkg.appendList(z)return z end a=function(a,f)if not f then return nil end local z={}local U={Client=a.Client;Board=a;Loaded=true}for Q,J in pairs(w)do local v=M.getValue(f,Q,w)U[Q]=v z[Q]=v end function U.Pull(a,v)local f=a.Client:MakeURL("\047\108\105\115\116\115\047"..a.RemoteId,J)local j=(Q.RequestInsist(f,Q.HttpMethod.GET,nil,true)).Body if not j then U=nil a=nil error("\091\084\114\101\108\108\111\047\076\105\115\116\046\080\117\108\108\093\058 \076\105\115\116 \104\097\115 \098\101\101\110 \100\101\108\101\116\101\100\033")end for Q,J in pairs(w)do local f=M.getValue(j,Q,w)if v or a[Q]==z[Q]then a[Q]=f end z[Q]=f end end function U.Push(J,M)local w=0 local v={}for Q,a in pairs(z)do if a~=J[Q]or M then v[Q]=J[Q]w=w+1 end end if w==0 then warn("\091\084\114\101\108\108\111\047\076\105\115\116\046\080\117\115\104\093\058 \078\111\116\104\105\110\103 \116\111 \099\104\097\110\103\101\046 \083\107\105\112\112\105\110\103\046")end local f=a.Client:MakeURL("\047\108\105\115\116\115\047"..J.RemoteId,{name=v.Name,closed=v.Archived,pos=v.Position})Q.RequestInsist(f,Q.HttpMethod.PUT,"\123\125",true)for Q,M in pairs(v)do z[Q]=M end end function U.Delete(M)local J=a.Client:MakeURL("\047\108\105\115\116\115\047"..M.RemoteId)Q.RequestInsist(J,Q.HttpMethod.DELETE,nil,true)U=nil end return setmetatable(U,v)end return{Public=f;Make=a}
--[[
    Name: List.lua
    From roblox-trello v2

    Description: Constructor for Trello lists.

    Copyright (c) 2019 Luis, David Duque

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
--]]

local HTTP = require(script.Parent.Parent.TrelloHttp)
local commons = require(script.Parent.Parent.Commons)

local fetchTable = {
    fields = {"id", "name", "closed", "pos"}
}
local indexDictionary = {
    RemoteId = "id",
    Name = "name",
    Archived = "closed",
    Position = "pos"
}

local TrelloListMeta = commons.generateMeta("TrelloList")

-- Local function prototype to make a board based on a body dump
local makeList

local TrelloList = {}

--[[**
    Creates a new list that is then attached to the given board.

    @param [t:String] name The name of the list.
    @param [t:Variant<int,"first","last">] position Where the list will be located. Pass "first" to put it on the start of the board, and "last" to put it on it's end.
    @param [t:TrelloBoard] board The board where the list will be placed.

    @returns [t:TrelloList] A new, empty list that can be used to store cards.
**--]]
function TrelloList.new(name, position, board)
    if not board or getmetatable(board) ~= "TrelloBoard" then
        error("[TrelloList.new]: Invalid board!", 0)
    elseif not name or name == "" or name:len() > 16384 then
        error("[TrelloList.new]: Invalid name! Make sure that the name is a non-empty string with less than 16384 characters.", 0)
    end

    local commitURL = board.Client:MakeURL("/lists", {
        name = name,
        pos = position,
        idBoard = board.RemoteId
    })

    local listDump = HTTP.RequestInsist(commitURL, HTTP.HttpMethod.POST, "{}", true)
    local list = makeList(listDump.Body)

    board._pkg.appendList(list)

    return list
end

makeList = function(board, data)
    if not data then
        return nil
    end

    local tracking = {}

    local trelloList = {
        Client = board.Client,
        Board = board,
        Loaded = true
    }

    for i, _ in pairs(indexDictionary) do
        local val = commons.getValue(data, i, indexDictionary)
        trelloList[i] = val
        tracking[i] = val
    end

    --[[**
        Fetches the metadata from Trello and updates the list's metadata. (Doesn't apply to cards)

        @param [t:Boolean] hard Whether to overwrite changes that you made, but did not push. (Defaults to false, which will do a soft pull)

        @returns [t:Void]
    **--]]
    function trelloList:Pull(hard)
        local commitURL = self.Client:MakeURL("/lists/"..self.RemoteId, fetchTable)

        local updatedData = HTTP.RequestInsist(commitURL, HTTP.HttpMethod.GET, nil, true).Body

        if not updatedData then
            -- FIXME: HANDLE THIS
            trelloList = nil
            self = nil
            error("[Trello/List.Pull]: List has been deleted!")
        end

        for i, _ in pairs(indexDictionary) do
            local val = commons.getValue(updatedData, i, indexDictionary)
            if hard or self[i] == tracking[i] then
                self[i] = val
            end
            tracking[i] = val
        end
    end

    --[[**
        Pushes the list's changes to Trello.

        @param [t:Boolean] force Whether to push all changes to the list even though nothing has been changed.

        @returns [t:Void]
    **--]]
    function trelloList:Push(force)
        local count = 0
        local commit = {}

        for i, v in pairs (tracking) do
            if v ~= self[i] or force then
                commit[i] = self[i]
                count = count + 1
            end
        end

        if count == 0 then
            warn("[Trello/List.Push]: Nothing to change. Skipping.")
        end

        local commitURL = board.Client:MakeURL("/lists/"..self.RemoteId, {
            name = commit.Name,
            closed = commit.Archived,
            pos = commit.Position
        })

        HTTP.RequestInsist(commitURL, HTTP.HttpMethod.PUT, "{}", true)

        for i, v in pairs(commit) do
            tracking[i] = v
        end
    end

    --[[**
        Deletes the label from Trello. All garbage collection is up to the developer to perform.

        @returns [t:Void]
    **--]]
    function trelloList:Delete()
        local commitURL = board.Client:MakeURL("/lists/" .. self.RemoteId)

        HTTP.RequestInsist(commitURL, HTTP.HttpMethod.DELETE, nil, true)

        trelloList = nil
    end

    return setmetatable(trelloList, TrelloListMeta)
end

return {Public = TrelloList, Make = makeList}