I've seen many news and opinions articles addressing this topic, but there is always the debate about bias and ideological preference. Considering how controversial this issue is in Spain and Catalonia, I would like to understand why some people claim it was, whilst other people claim it has always been part of Spain.
This is an extremely vapourous question, as it goes to the fundamental concepts of historical continuity and what is a country, that's why it is so difficult to answer. The short answer would be that Catalonia has never been an independent country, but that would require a number of asterisks here and there.
Starting from the beginning, the denomination of Catalonia, and of the Catalan people only emerges in the late 12th century. Would it be correct to apply the term to the realities that were in that region prior to that? Only in the geographic sense, and by that I mean in order for the modern reader to understand the situation better. Let's jump a bit farther back in time, to the 10th century.
In the late 9th and 10th centuries, part of the territory that nowadays corresponds to modern Catalonia was part of the Frankish Empire, and of the Frankish Kingdom, it was a region called Marca Hispanica, a frontier territory governed by a marquess in the name of the Frankish kings. The position was not hereditary originally, but eventually dynasties arose, and the Bellonids became the hereditary counts of Ausona, Gerona, Barcelona, and a number of places. They were still vassals to the Frankish Kings, but governed with a lot of independence, as they were far from the Court, and separated from the Frankish realm by a big mountainous chain, the Pyrenees. Come the year 985, and Al-Mansur, visir to the Caliph of Cordova, attacks Barcelona, sacks it, and pillages the rest of the territory in modern day Catalonia. The count of Barcelona, screwed as he was, asked his liege for help, the help that he was due. Help never arrived. Three years later, the new Frankish king called his vassals to pledge fealty to him, and that included the count of Barcelona, who did not show up: if the kings of the Franks do not fulfill their duty of protection, then there is no point in being his vassal.
From that point onwards, we can start talking about an independent county of Barcelona, which at that time would be some 1/2 of present day Catalonia. This is, however, still a matter of feudalism, and at a point where "Catalonia" was not even a thing.
If we jump forward in time, to the year 1137, we have a King of Aragon in big trouble, so he promises his daughter in marriage to the Count of Barcelona, who then becomes "dominator" of Aragon. Should the King's daughter die without having provided an heir, then the Count of Barcelona would take over and be the rightful ruler of Aragon, for him and for his successors. They were happily married, and succession was not a problem. From the year 1137, the county of Barcelona was integrated into the Crown of Aragon, or "patrimony of the King of Aragon", but functioned differently: they had their own privileges, laws, customs, coinage, you name it. In the 14th century things get much clearer and better codified, and we start to have some political sense of the Principality of Catalonia, which was one of the consituent realms of the Crown of Aragon (think of it as the United Kingdom, if you wish).
Come the late 15th century, Isabel of Castile and Ferdinand of Aragon get happily married, and from then onwards we have a personal union between the crowns of Castile and Aragon: id est, the same person in King of Castile and King of Aragon, but the king of Castile and the king Aragon are two different offices. It's even more complex than that, considering that culturally speaking all the peoples of both realms were spaniards, and there was a certain unity, though not a political one. It's really complex.
I'll leave this comment at this length, and elaborate further on the matter of the Catalan Insurrection of 1640, which deserves some particularisation, as it is one of the main national myths of Catalonia.
Back in the saddle, so let's talk about the Reapers' War, also known as the Catalan uprising, but with some precedent first.
Willing to strengthen the Empire's military in order to be able to cope with the many wars, the Count-Duke of Olivares proposed the Unión de Arms (Union of Arms), a system under which all of the territories under Philip IV's authority would contribute to the war efforts according to their respective population and wealth. This would have meant having an army of 140,000 men, which should just about suffice for all of the troubles. The whole idea was entirely rejected by the parliaments of the Crown of Aragon, not willing to compromise men or wealth for the wars, which lead to a lot of discontent, best summarised by Quevedo's verses:
En Navarra y Aragón no hay quien tribute un real; Cataluña y Portugal son de la misma opinión; sólo Castilla y León y el noble pueblo andaluz llevan a cuestas la cruz. (In Navarre or Aragon, none contributes a real, Catalonia and Portugal are of the same opinion; only Castille and Leon, and the noble Andalusian folk, carry on their shoulders the brunt).
Come the year of 1635, and Catalonia finds the Union of Arms right at home. In 1639, the French armies invade the Roussillon. Naturally, the King asks the Catalan parliament to raise an army at once, and levy some taxes in order to finance it, as it would be the fastest course of action until a substantial army can come into play. The Catalan parliament rejected the idea, which resulted in an exasperated outburst by Olivares:
Catalonia is a province that there is no King in the world that has one like it. If the enemies attack it, its King shall defend it without them contributing their part, nor exposing their people to any perils. He must bring the army from outside, has to maintain it, recover the lost places, and this army, not with the enemies expelled or before their expulsion can camp, or, nor shall the province lodge it... One has to look whether the constitutions say this, the customs say that, when this is about the supreme law: the survival of the province.
The army being discontent with all this, getting paid late, not being able to camp, it turned to pillage and extortions, which only made matters worse, creating popular outrage. Come the month of May of 1640, and rebellion erupts: the viceroy, Count of Santa Coloma, is murdered, the rebels control Barcelona. Pau Claris, first deputy of the Deputation of the General, proclaims the Catalan Republic. He manages to secure the support of France, and in September of that same year a pact is signed with France: Catalonia shall be a republic under the tutelage of France.
The war, by the end of the year was going badly for the Catalans, with the royal army of Philip IV taking Cambrils, Tarragona, and Martorell. The bad situation made the Catalan authorities cave in to the French demands, and on January 23rd of 1641 they swore Louis XIV of France as Count of Barcelona, turning Catalonia into a French territory. With Spain involved heavily in the War of the Thirty Years, the Reapers' War between France and Spain lasted for a long time, with different moments of back and forth. After the Peace of Westphalia, France lost interest in the matters of Catalonia, which allowed for a solid advance of the Spanish armies. In 1651, the armies of Philip IV under the command of his bastard son Juan José de Austria besiege Barcelona, and conquer it in 1652. The Catalan authorities swear Philip IV as Count of Barcelona. France, however, keeps controlling the Roussillon, a matter that would be settled in the Treaty of the Pyrenees, where Spain ceded the county of Roussillon to France.
So, in the second half of the year 1640, Catalonia was a Republic under the tutelage of the King of France, which is as close to independence as Catalonia has ever been.
Important note: the Catalan parliament (les Corts) was not a democratic institution, it was feudal in its very nature, composed of members of three branche, called arms: the ecclesiastical arm, the military arm, and the royal arm. These Corts had a permanent deputation called Diputació del General (the Deputation of the General) that managed the collection of the General capitation tax when the parliament was not in session. Its "first deputy" or "resident deputy" was the seniormost (by rank) member of the ecclesiastic arm as a matter of protocol, recognising the primacy of the Church.