From 66fcb190ad52aa7e5fc7e0055fed392a569c1972 Mon Sep 17 00:00:00 2001
From: Nathan-SR <144961007+Nathan-SR@users.noreply.github.com>
Date: Tue, 29 Oct 2024 18:48:25 -0400
Subject: matching algorithm working

---
 .../nodes/DataVizBox/DocCreatorMenu/Template.tsx    | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

(limited to 'src/client/views/nodes/DataVizBox/DocCreatorMenu/Template.tsx')

diff --git a/src/client/views/nodes/DataVizBox/DocCreatorMenu/Template.tsx b/src/client/views/nodes/DataVizBox/DocCreatorMenu/Template.tsx
index 486fe7f7e..708170359 100644
--- a/src/client/views/nodes/DataVizBox/DocCreatorMenu/Template.tsx
+++ b/src/client/views/nodes/DataVizBox/DocCreatorMenu/Template.tsx
@@ -24,33 +24,44 @@ export class Template {
     }
 
     setupMainField = (templateInfo: FieldSettings) => {
-        return new DynamicField(templateInfo, ViewType.FREEFORM, 0);
+        return new DynamicField(templateInfo, 0);
     }
 
     isValidTemplate = (cols: Col[]) => {
-        return this.maxMatches(this.getMatches(cols)) === this.contentFields.length;
+        const matches: number[][] = this.getMatches(cols);
+        const maxMatches: number = this.maxMatches(matches);
+        return maxMatches === this.contentFields.length;
     }
 
-    getMatches = (cols: Col[]) => {
-        const matches: number[][] = Array(this.contentFields.length)
+    getMatches = (cols: Col[]): number[][] => {
+        const numFields = this.contentFields.length;
+
+        if (cols.length !== numFields) return [];
+
+        const matches: number[][] = Array(numFields)
             .fill([])
             .map(() => []);
 
         this.contentFields.forEach((field, i) => {
-            matches[i].concat(field.matches(cols));
+            matches[i] = (field.matches(cols));
         });
 
         return matches;
     }
 
     maxMatches = (matches: number[][]) => {
+        if (matches.length === 0) return 0;
+
         const fieldsCt = this.contentFields.length;
         const used: boolean[] = Array(fieldsCt).fill(false);
         const mt: number[] = Array(fieldsCt).fill(-1);
 
+        console.log(matches, fieldsCt);
+
         const augmentingPath = (v: number): boolean => {
             if (used[v]) return false;
             used[v] = true;
+
             for (const to of matches[v]) {
                 if (mt[to] === -1 || augmentingPath(mt[to])) {
                     mt[to] = v;
-- 
cgit v1.2.3-70-g09d2

