Here is a sentence that I came across in Yotsuba, 恵那が結婚して出て行く時に使おうと思ってたんだ. I believe the English translation would be something like, "When Ena (恵那) gets married and leaves, I think I'll use this. I know that と思って means that something has been thought of for some duration but what does the たんだ stand for? I believe んだ is the connective copula for explanatory purposes. So the translation would be something like "It is that, I think I will use this when Ena goes off and gets married." But why is た used? Is this acting as something for past tense or is it needed to connect んだ somehow? Could it mean, "I had thought"?
Any help would be appreciated. I'm starting to notice these types of past tense uses more but still can't 100% wrap my head around them.