I am not really that familiar with the Middle Ages status of Catalonia (County of Barcelona and after dynastic marriage joined with Kingdom of Aragon), but I am suspecting that there wasn't any real difference between them and Portugal. County of Barcelona joined Aragon through dynastic union, and then Aragon and Castille also joined through dynastic union (with both sides wanting it). There were chances that similar dnyastic union happens with Portugal and Castille, but ultimately they all fell through.
For the 17th century Restoration I will redirect you to this answer by /u/kieslowskifan and this answer by /u/itsalrightwithme.
TL;DR is that Catalonia tried declaring independence first, and Spain directed their limited troops and resources to quell that rebellion, and Portugal used this opportunity to themselves separate. Ultimately Portugal succeeded, while Catalonia did not