public function primaryChildFamily(): ?Family
    {
        $families = $this->childFamilies();
        switch ($families->count()) {
            case 0:
                return null;
            case 1:
                return $families[0];
            default:
                // If there is more than one FAMC record, choose the preferred parents:
                // a) records with '2 _PRIMARY'
                foreach ($families as $fam) {
                    $famid = $fam->xref();
                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 _PRIMARY Y)/", $this->gedcom())) {
                        return $fam;
                    }
                }
                // b) records with '2 PEDI birt'
                foreach ($families as $fam) {
                    $famid = $fam->xref();
                    if (preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI birth)/", $this->gedcom())) {
                        return $fam;
                    }
                }
                // c) records with no '2 PEDI'
                foreach ($families as $fam) {
                    $famid = $fam->xref();
                    if (!preg_match("/\n1 FAMC @{$famid}@\n(?:[2-9].*\n)*(?:2 PEDI)/", $this->gedcom())) {
                        return $fam;
                    }
                }

                // d) any record
                return $families[0];
        }
    }