Format a Swedish phone number In some parts of the world, phone numbers are fixed-length, apparently. That sounds boring, though.

For this challenge, your task is to take the digits of a what is presumed to be a valid phone number, and format it based on some simplified rules around what a phone number looks like.

Input format

Your input will consist of nothing except the digits 0 through 9, and the input will contain between five and ten of them (both inclusive). Every input will be a valid phone number, according to the following simplified rules:


A full phone number consists of eight to ten digits and can be divided into an optional area code of two to four digits (the first of which is 0), and a mandatory subscriber number of five to eight digits (the first of which is not 0)
When calling someone from a landline phone who has the same area code as you, you do not need to dial the area code. Thus, your program must be able to handle inputs consisting only of a subscriber number with no area code. 
The subscriber number consists of 5 to 8 digits, and the first one isn't 0.
The area code, if present, begins with 0, and has 2 to 4 digits. For this challenge's purposes, the length depends on the second digit:


The second digit of the area code will never be 0.
If the second digit is 8, the entire area code will be 2 digits long (so the entire area code will be 08)
If the second digit is 1, 2, 3, 4, or 7, the area code will be 3 digits long.
If the second digit is 5, 6, or 9, the area code will be 4 digits long.

However, for some inputs, the above cutoff point would cause the subscriber number to be too short or begin with a 0. Since we know the input is a valid phone number (because I say so) we have to conclude that the length of the area code is different from the above. If the length listed above does not work, you should pick a length which works, preferring 3-digit codes over 4-digit codes, and preferring either of those to 2-digit codes. You will not need to handle inputs like 0801234, which are invalid for all those cutoff points.


Output format

Your output should be the area code, followed by a dash (only if an area code is present), followed by the subscriber number which should be grouped in one of the following manners (with the groups separated by a single space):

xxx xxx xx
xxx xx xx
xx xx xx
xxx xx


That means numbers with area codes specified will have one of the following formats:

0x-xxx xxx xx
0x-xxx xx xx
0x-xx xx xx
0xx-xxx xx xx
0xx-xx xx xx
0xx-xxx xx
0xxx-xx xx xx
0xxx-xxx xx


Test cases

All numbers were randomly generated, and some of them could potentially be in use, so while I don't expect any of them to belong to anyone in particular, please don't try to call them.

27983516    -&gt;  279 835 16     # No area code, 8 digits
6278304     -&gt;  627 83 04      # No area code, 7 digits
627830      -&gt;  62 78 30       # No area code, 6 digits
11865       -&gt;  118 65         # No area code, 5 digits
067934821   -&gt;  0679-348 21    # Simple 4+5
0927586130  -&gt;  0927-58 61 30  # Simple 4+6   
0374821096  -&gt;  037-482 10 96  # Simple 3+7
042807961   -&gt;  042-80 79 61   # Simple 3+6
04629873    -&gt;  046-298 73     # Simple 3+5
0897106384  -&gt;  08-971 063 84  # Simple 2+8
081859302   -&gt;  08-185 93 02   # Simple 2+7
08379451    -&gt;  08-37 94 51    # Simple 2+6
0480936572  -&gt;  0480-93 65 72  # Area code couldn't be 048 to prevent subscriber number starting with 0
0957024168  -&gt;  095-702 41 68  # Area code couldn't be 0957 for the same reason
06057291    -&gt;  060-572 91     # Area code couldn't be 0605 because of subscriber number's length
04809714    -&gt;  04-80 97 14    # Area code could neither be 048 nor 0480 didn't help
080062517   -&gt;  0800-625 17    # Area code could neither be 08 nor 080
09106574    -&gt;  09-10 65 74    # Area code had to be shortened twice from 0910 because shortening it to 091 didn't work


This is code-golf, shortest code in each language wins.
Retina, 78 73 bytes
(0(8|[569].?.?|..?0?)(?=[^0]....))?(...?)?(...?)(..)$
$1$#1*-$3$#3* $4 $5

Inspired by both @mazzy's PowerShell answer and @NahuelFouilleul's Perl 5 answer, so make sure to upvote them both as well!
-5 bytes thanks to @Neil.
Try it online.
Explanation:

Match the digit groups like this:
(                                )? # An optional group 1, which consists of:
 0                                  #  A leading 0
  ( |         |     )               #  followed by either (ignored inner group 2):
   8                                #   A single 8
    |[569]                          #   or one of 569,
          .?.?                      #      with one or two optional chars
              |.                    #   or any character,
                .?                  #      with one optional character,
                  0?                #      and an optional trailing 0
                     (?=        )   #  followed by an uncaptured inner group with:
                        [^0]        #   A character which is NOT a 0
                            ....    #   followed by any four characters
(    )?                             # An optional group 3, which consists of:
 ..                                 #  Two characters,
   .?                               #  followed by an optional character
(    )                              # A mandatory group 4,
 ...?                               #  which is similar as group 3
(  )                                # Followed by a mandatory group 5
    $                               # (at the end of the string), which consists of:
 ..                                 #  Any two characters

Then replace it with:
$1                                  # The content of group 1 (possibly empty)
  $#1*                              # If group 1 isn't empty:
      -                             #  A &quot;-&quot;
       $3                           # The content of group 3 (possibly empty)
         $#3*                       # If group 3 isn't empty:
             &lt;sp&gt;                   #  A &quot; &quot;
                 $4                 # The content of group 4
                   &lt;sp&gt;             # A &quot; &quot;
                       $5           # The content of group 5