How to format Brazilian phone numbers in Google Sheets?

I have two possible phone numbers:

One with 9 digits (mobile) and 8 digits (landline), and I need to insert `-` after 4 or 5 digits so they appear as `0000-0000` when 8 digits, and `00000-0000` when they are 9 digits.

Also, need to get a country and state codes like this `+00 (00)` the numbers inside the parentheses are the state codes.

I managed to get a formula that works with one or another, but not both. Can anyone help?

`"+"?? (##) ????-????` > Works for 8 digit numbers

`"+"## (##) ?????-????` > Works with 9 digits.

``````=IF(AND(A1>9999999;  A1<=99999999);  "+"&"55"&" ("&"00"&") "&LEFT(A1;4)&"-"&RIGHT(A1;4);
and then `"00"` / `"55"` can be referenced from a cell for ultimate comfort
• @Freedo >> if A1 is number larger then 9999999 and smaller or equal to 99999999 (eg. all 8 digit possibilities you can ever have), then add `"+"` followed by `"55"` followed by `" ("` followed by `"00"` followed by `") "` followed by 8 digit number separated in the middle with `"-"` (eg 4 digits from left side of the number from A1 cell and 4 digits from right side of the number from A1 cell) – user0 Jul 29 '18 at 7:35