private function splitMessages(&$parsed, &$errors): array
    {
        // init
        $messages = [];
        $tmpmsg = [];
        $service = [];
        $hasInterchangeDelimiters = 0;
        $hasMessageDelimiters = 0;

        foreach ($parsed as $c => $segment) {
            switch ($segment[0]) {
                case 'UNB':
                    $hasInterchangeDelimiters = 0;
                    $hasInterchangeDelimiters++;
                    $service['UNB'] = $segment;
                    break;
                case 'UNZ':
                    $hasInterchangeDelimiters--;
                    if ($hasInterchangeDelimiters != 0) {
                        $sid = ($hasInterchangeDelimiters < 0) ? "UNB" : "UNZ";
                        $errors[] = [
                            "text"      => $this->messageTextConf['MISSINGINTERCHANGEDELIMITER'],
                            "position"  => $c,
                            "segmentId" => $sid,
                        ];
                    }
                    $service['UNZ'] = $segment;
                    break;
                case 'UNH':
                    $hasMessageDelimiters = 0;
                    $hasMessageDelimiters++;
                    $tmpmsg = [$segment];
                    break;
                case 'UNT':
                    $hasMessageDelimiters--;
                    $tmpmsg[] = $segment;
                    $messages[] = $tmpmsg;
                    if ($hasMessageDelimiters != 0) {
                        $sid = ($hasMessageDelimiters < 0) ? "UNH" : "UNT";
                        $errors[] = [
                            "text"      => $this->messageTextConf['MISSINGMESSAGEDELIMITER'],
                            "position"  => $c,
                            "segmentId" => $sid,
                        ];
                    }
                    break;
                default:
                    $tmpmsg[] = $segment;
                    break;
            }
        }

        if ($hasInterchangeDelimiters != 0) {
            $sid = ($hasInterchangeDelimiters < 0) ? "UNB" : "UNZ";
            $errors[] = [
                "text"      => $this->messageTextConf['MISSINGINTERCHANGEDELIMITER'],
                "position"  => $c ?? '',
                "segmentId" => $sid,
            ];
        }

        if ($hasMessageDelimiters != 0) {
            $sid = ($hasMessageDelimiters < 0) ? "UNH" : "UNT";
            $errors[] = [
                "text"      => $this->messageTextConf['MISSINGMESSAGEDELIMITER'],
                "position"  => $c ?? '',
                "segmentId" => $sid,
            ];
        }

        $messages['service'] = $service;

        return $messages;
    }