From 1effe97c83585e4ba7bab68bafdbee733a6a25b4 Mon Sep 17 00:00:00 2001
From: "(quasar) nebula" <qznebula@protonmail.com>
Date: Thu, 14 Nov 2024 17:51:48 -0400
Subject: data: stub real results for find.mixed

Works by name, not by directory. Also no caching, and some stuff
common with findHelper should be factored out.
---
 src/find.js | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 63 insertions(+), 8 deletions(-)

(limited to 'src')

diff --git a/src/find.js b/src/find.js
index fa5f6904..b2d32f80 100644
--- a/src/find.js
+++ b/src/find.js
@@ -262,15 +262,70 @@ export const boundFindOptions = Symbol.for('find.boundFindOptions');
 const mixedFindStore = new Map();
 
 function findMixedHelper(config) {
-  const keys = Object.keys(config);
-  const tokens = Object.values(config);
-  const specKeys = tokens.map(token => token[findTokenKey]);
-  const specs = specKeys.map(specKey => findFindSpec(specKey));
+  const
+    keys = Object.keys(config), referenceTypes = keys,
+    tokens = Object.values(config),
+    specKeys = tokens.map(token => token[findTokenKey]),
+    specs = specKeys.map(specKey => findFindSpec(specKey));
+
+  return (fullRef, data, {mode = 'warn'} = {}) => {
+    // TODO: Cache stuff below by identity of data
+
+    const byName = Object.create(null);
+    const multipleNameMatches = Object.create(null);
+
+    for (const spec of specs) {
+      processAvailableMatchesByName(data, {
+        ...spec,
+
+        results: byName,
+        multipleNameMatches,
+      });
+    }
+
+    // TODO: Factor out this common behavior w/ findHelper
+
+    const regexMatch = fullRef.match(keyRefRegex);
+    if (!regexMatch) {
+      return warnOrThrow(mode,
+        `Malformed link reference: "${fullRef}"`);
+    }
+
+    const {key: keyPart, ref: refPart} = regexMatch.groups;
+
+    if (keyPart && !referenceTypes.includes(keyPart)) {
+      return warnOrThrow(mode,
+        `Reference starts with "${keyPart}:", expected ` +
+        referenceTypes.map(type => `"${type}:"`).join(', '));
+    }
 
-  return () => {
-    console.log(`I would do something with:`);
-    console.log(specs);
-    return null;
+    const normalizedName =
+      (keyPart
+        ? null
+        : refPart.toLowerCase());
+
+    const match =
+      (keyPart
+        ? null /* TODO: Do something */
+        : byName[normalizedName]);
+
+    if (!match && !keyPart) {
+      if (multipleNameMatches[normalizedName]) {
+        return warnOrThrow(mode,
+          `Multiple matches for reference "${fullRef}". Please resolve:\n` +
+          multipleNameMatches[normalizedName]
+            .map(match => `- ${inspect(match)}\n`)
+            .join('') +
+          `Returning null for this reference.`);
+      }
+    }
+
+    if (!match) {
+      return warnOrThrow(mode,
+        `Didn't match anything for ${colors.bright(fullRef)}`);
+    }
+
+    return match;
   };
 }
 
-- 
cgit 1.3.0-6-gf8a5