@lunora/codegen 1.0.0-alpha.81 → 1.0.0-alpha.83

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. package/dist/index.d.mts +1125 -1074
  2. package/dist/index.d.ts +1125 -1074
  3. package/dist/index.mjs +1 -1
  4. package/dist/packem_shared/DEFAULT_TARGET-CH5disA3.mjs +1 -0
  5. package/dist/packem_shared/GENERATED_HEADER-oy7DTIDi.mjs +1 -0
  6. package/dist/packem_shared/{OPENRPC_VERSION-DfKtsdPS.mjs → OPENRPC_VERSION-K3RTK2CP.mjs} +1 -1
  7. package/dist/packem_shared/SCHEMA_SNAPSHOT_FILENAME-ZIqaP_TZ.mjs +9 -0
  8. package/dist/packem_shared/SchemaSnapshotParseError-BJkhTEpi.mjs +16 -0
  9. package/dist/packem_shared/{buildOpenApiDocument-D46nZ7D_.mjs → buildOpenApiDocument-Blf3mpIz.mjs} +1 -1
  10. package/dist/packem_shared/describeErrorLevelFindings-IhNx9gPr.mjs +1 -0
  11. package/dist/packem_shared/discover-queries-Bhu9SNsy.mjs +1 -0
  12. package/dist/packem_shared/discoverCrons-DLwFH_K5.mjs +1 -0
  13. package/dist/packem_shared/discoverMaskProcedures-ZewKJLXx.mjs +1 -0
  14. package/dist/packem_shared/{discoverQueries-2A0lbpxV.mjs → discoverQueries-g4qZzLpl.mjs} +1 -1
  15. package/dist/packem_shared/discoverSchema-4nJgoLaJ.mjs +1 -0
  16. package/dist/packem_shared/{emit-CCw5fiXq.mjs → emit-B7IUlWwi.mjs} +371 -248
  17. package/dist/packem_shared/{emitApp-DEHN578T.mjs → emitApp-jRD2D9kI.mjs} +112 -37
  18. package/dist/packem_shared/readPackageDependencies-CogJX1Ia.mjs +1 -0
  19. package/package.json +10 -10
  20. package/dist/packem_shared/DEFAULT_TARGET-BYPEwgZu.mjs +0 -1
  21. package/dist/packem_shared/GENERATED_HEADER-CR3xA8yO.mjs +0 -1
  22. package/dist/packem_shared/SCHEMA_SNAPSHOT_FILENAME-DjpfF4p7.mjs +0 -12
  23. package/dist/packem_shared/SchemaSnapshotParseError-0KRzSjo4.mjs +0 -14
  24. package/dist/packem_shared/discover-queries-Ctpk9SA-.mjs +0 -1
  25. package/dist/packem_shared/discoverCrons-CwtPaymU.mjs +0 -1
  26. package/dist/packem_shared/discoverMaskProcedures-DQ-4yUUK.mjs +0 -1
  27. package/dist/packem_shared/discoverSchema-DUl8rbP3.mjs +0 -1
@@ -1 +0,0 @@
1
- import{Node as n,SyntaxKind as P}from"ts-morph";import{listLunoraSourceFiles as u,lunoraRelativePath as E,classifyProcedureCall as g}from"./discoverFunctions-CVYkUBTm.mjs";const S=t=>{if(!n.isCallExpression(t))return!1;const e=t.getExpression();return n.isIdentifier(e)?e.getText()==="mask":n.isPropertyAccessExpression(e)?e.getName()==="mask":!1},f=t=>{if(n.isPropertyAssignment(t)||n.isShorthandPropertyAssignment(t)||n.isMethodDeclaration(t)||n.isGetAccessorDeclaration(t))return t.getName()},v=t=>{const e=t.getArguments()[0];if(!e||!n.isObjectLiteralExpression(e))return[];const s=[];for(const r of e.getProperties()){const o=f(r);if(o===void 0||!n.isPropertyAssignment(r))continue;const i=r.getInitializer();if(!(!i||!n.isObjectLiteralExpression(i)))for(const a of i.getProperties()){const c=f(a);c!==void 0&&s.push({column:c,table:o})}}return s},p=t=>{const e=[];let s=t;for(;n.isCallExpression(s);){const r=s.getExpression();if(!n.isPropertyAccessExpression(r))break;if(r.getName()==="use"){const o=s.getArguments()[0];o&&S(o)&&e.push(o)}s=r.getExpression()}return e},A=t=>{const e=p(t);return{maskColumns:e.flatMap(s=>v(s)),usesMask:e.length>0}},y=new Set(["findFirst","findFirstOrThrow","findMany","get","query"]),k=new Set(["delete","insert","patch","replace"]),x=(t,e)=>{const s=t.getExpression();if(!n.isPropertyAccessExpression(s)||!e.has(s.getName()))return!1;const r=s.getExpression();return n.isPropertyAccessExpression(r)?r.getName()==="db":n.isIdentifier(r)&&r.getText()==="db"},b=t=>{const e=t.getArguments()[0];return e&&n.isStringLiteral(e)?e.getLiteralText():""},L=t=>{const e=new Set,s=new Set;for(const r of t.getDescendantsOfKind(P.CallExpression))if(x(r,y)){const o=b(r);o!==""&&e.add(o)}else if(x(r,k)){const o=b(r);o!==""&&s.add(o)}return{tablesRead:[...e],tablesWritten:[...s]}},M=(t,e)=>{if(!n.isVariableDeclaration(t))return;const s=t.getInitializer();if(!s||!n.isCallExpression(s))return;const r=g(s);if(!r)return;const o=r.receiver?A(r.receiver):{maskColumns:[],usesMask:!1},{tablesRead:i,tablesWritten:a}=L(t);return{exportName:t.getName(),file:e,maskColumns:o.maskColumns,tablesRead:i,tablesWritten:a,usesMask:o.usesMask,visibility:r.visibility}},O=(t,e)=>{const s=[];for(const r of u(e)){const o=t.getSourceFile(r)??t.addSourceFileAtPath(r),i=E(e,r);for(const a of o.getVariableStatements())if(a.isExported())for(const c of a.getDeclarations()){const l=M(c,i);l&&s.push(l)}}return s},h=t=>{if(n.isPropertyAssignment(t)){const e=t.getInitializer();if(e&&n.isStringLiteral(e)){const s=e.getLiteralText();if(s==="redact"||s==="hash")return s}}return"custom"},C=t=>{const e=t.getArguments()[0];if(!e||!n.isObjectLiteralExpression(e))return[];const s=[];for(const r of e.getProperties()){const o=f(r);if(o===void 0||!n.isPropertyAssignment(r))continue;const i=r.getInitializer();if(!(!i||!n.isObjectLiteralExpression(i)))for(const a of i.getProperties()){const c=f(a);c!==void 0&&s.push({column:c,strategy:h(a),table:o})}}return s},N=t=>{const e=[];for(const s of t.getVariableStatements())if(s.isExported())for(const r of s.getDeclarations()){const o=r.getInitializer(),i=o&&n.isCallExpression(o)?g(o):void 0;i?.receiver&&e.push(i.receiver)}return e},j=(t,e)=>{const s=new Map;for(const r of u(e)){const o=t.getSourceFile(r)??t.addSourceFileAtPath(r);for(const i of N(o))for(const a of p(i))for(const c of C(a)){const l=`${c.table}\0${c.column}`;s.has(l)||s.set(l,c)}}return{columns:[...s.values()]}},F=(t,e,s)=>{const r=t.getArguments()[0];if(!r||!n.isObjectLiteralExpression(r))return[];const o=[];for(const i of r.getProperties()){const a=f(i);if(a===void 0||!n.isPropertyAssignment(i))continue;const c=i.getInitializer();if(!(!c||!n.isObjectLiteralExpression(c)))for(const l of c.getProperties()){const m=f(l);if(m===void 0)continue;const d=h(l);d!=="custom"&&o.push({column:m,exportName:e,file:s,line:l.getStartLineNumber(),strategy:d,table:a})}}return o},I=(t,e)=>{if(!n.isVariableDeclaration(t))return[];const s=t.getInitializer(),r=s&&n.isCallExpression(s)?g(s):void 0;return r?.receiver?p(r.receiver).flatMap(o=>F(o,t.getName(),e)):[]},w=(t,e)=>{const s=[];for(const r of u(e)){const o=t.getSourceFile(r)??t.addSourceFileAtPath(r),i=E(e,r);for(const a of o.getVariableStatements())if(a.isExported())for(const c of a.getDeclarations())s.push(...I(c,i))}return s};export{O as default,j as discoverMaskMetadata,w as discoverMaskStrategies};
@@ -1 +0,0 @@
1
- import{LunoraError as T}from"@lunora/errors";import{Node as s,SyntaxKind as j}from"ts-morph";import{diagnosticAt as u}from"./CodegenDiagnosticError-DPezpZTz.mjs";import{c as w}from"./parse-validator-_jDrFxwt.mjs";import{createRequire as N}from"node:module";import{join as O}from"node:path";const $=e=>{switch(typeof e){case"bigint":return e.toString();case"boolean":case"number":return String(e);case"string":return JSON.stringify(e);default:return e===null?"null":void 0}},_=e=>({...e?.defaultValue!==void 0||e?.defaultFn!==void 0||e?.serverDefault!==void 0?{hasDefault:!0}:{},...e?.onUpdateFn===void 0?{}:{hasOnUpdate:!0},notNull:e?.notNull??!0,...e?.unique===!0?{unique:!0}:{}}),f=e=>{const r=e?._meta??{},t=e?.kind??"unknown",n={column:_(r.column),kind:t};switch(t){case"array":case"optional":{n.inner=f(r.inner);break}case"id":{n.tableName=r.tableName;break}case"literal":{n.literalValue=$(r.value);break}case"object":{const i=r.shape??{};n.shape=Object.fromEntries(Object.entries(i).map(([o,a])=>[o,f(a)]));break}case"record":{n.valueType=f(r.valueValidator),r.keyValidator!==void 0&&(n.keyType=f(r.keyValidator));break}case"storage":{typeof r.bucket=="string"&&(n.bucket=r.bucket);break}case"union":{const i=r.members??[];n.members=i.map(o=>f(o));break}}return n},M=e=>e?.kind==="global"?"global":e?.kind==="shardBy"&&typeof e.field=="string"?{field:e.field,kind:"shardBy"}:"root",z=(e,r)=>{const t=[];e.relationMap&&Object.keys(e.relationMap).length>0&&t.push("relations"),e.rankIndexes&&e.rankIndexes.length>0&&t.push("rank indexes"),e.vectorIndexes&&e.vectorIndexes.length>0&&t.push("vector indexes"),t.length>0&&console.warn(`@lunora/codegen: package schema-extension table "${r}" declares ${t.join(" + ")}, which are not yet introspected from a node_modules extension — they will be absent from the generated types.`)},D=(e,r)=>{const t=e.shape??{},n=(e.indexes??[]).map(i=>({fields:i.fields,name:i.name,...i.unique?{unique:!0}:{}}));return z(e,r),{...e.isExternallyManaged?{externallyManaged:!0}:{},geoIndexes:[],indexes:n,name:r,rankIndexes:[],relations:[],searchIndexes:(e.searchIndexes??[]).map(i=>({field:i.field,...i.filterFields?{filterFields:i.filterFields}:{},...i.language===void 0?{}:{language:i.language},...i.staged===void 0?{}:{staged:i.staged},...i.strategy===void 0?{}:{strategy:i.strategy},name:i.name})),shape:Object.fromEntries(Object.entries(t).map(([i,o])=>[i,f(o)])),shardMode:M(e.shardMode),vectorIndexes:[]}},B=e=>{const{key:r,tables:t}=e;if(typeof r!="string"||typeof t!="object"||t===null)return;const n=Object.entries(t).map(([a,l])=>D(l,a)),i=e.vectorIndexes??{},o=Object.entries(i??{}).map(([a,l])=>({...l.dimensions===void 0?{}:{dimensions:l.dimensions},...l.field===void 0?{}:{field:l.field},...l.metadata===void 0?{}:{metadata:l.metadata},...l.metric===void 0?{}:{metric:l.metric},name:a,table:l.table??""}));return{bareTables:n,bareVectorIndexes:o,key:r}},q=e=>{if(s.isIdentifier(e))return e.getText();if(s.isPropertyAccessExpression(e)){const r=e.getExpression();return s.isIdentifier(r)?r.getText():void 0}},F=e=>{const r=q(e);if(r===void 0)return;const t=s.isPropertyAccessExpression(e)?e.getName():void 0,n=t===void 0?[]:[t];for(const i of e.getSourceFile().getImportDeclarations()){const o=i.getModuleSpecifierValue();for(const a of i.getNamedImports())if((a.getAliasNode()?.getText()??a.getName())===r)return{importedName:a.getName(),moduleSpecifier:o,propertyPath:n};if(i.getDefaultImport()?.getText()===r)return{importedName:"default",moduleSpecifier:o,propertyPath:n};if(i.getNamespaceImport()?.getText()===r&&t!==void 0)return{importedName:t,moduleSpecifier:o,propertyPath:[]}}},C=(e,r)=>{let t=e[r.importedName];for(const n of r.propertyPath){if(typeof t!="object"||t===null)return;t=t[n]}return t},V=(e,r)=>{const t=F(e);if(t)try{const n=N(O(r,"noop.cjs")),i=n.resolve(t.moduleSpecifier),o=n(i),a=C(o,t);return typeof a!="object"||a===null?void 0:B(a)}catch{return}},R=new Set(["cosine","dot-product","euclidean"]),J=new Set(["cascade","restrict","set null"]),v=new Set(["delete","get","insert","normalizeId","patch","query","replace","system"]),U=(e,r)=>{if(v.has(e))throw u(r,`table name "${e}" is reserved — it collides with a \`ctx.db\` member (one of ${[...v].map(t=>`"${t}"`).join(", ")}). Rename the table.`)},d=(e,r)=>{const t=e.getProperty(r);if(t&&s.isPropertyAssignment(t)){const n=t.getInitializer();if(n&&s.isStringLiteral(n))return n.getLiteralText()}},m=(e,r)=>{const t=e.getProperty(r);if(t&&s.isPropertyAssignment(t)){const n=t.getInitializer();if(n&&s.isNumericLiteral(n))return Number(n.getLiteralText())}},K=(e,r)=>{const t=e.getProperty(r);if(t&&s.isPropertyAssignment(t)){const n=t.getInitializer();if(n&&(s.isTrueLiteral(n)||s.isFalseLiteral(n)))return n.getLiteralValue()}},h=(e,r)=>{const t=e.getProperty(r);if(t&&s.isPropertyAssignment(t)){const n=t.getInitializer();if(n&&s.isArrayLiteralExpression(n))return n.getElements().filter(i=>s.isStringLiteral(i)).map(i=>i.getLiteralText())}},E=e=>e&&R.has(e)?e:void 0,G=e=>e&&J.has(e)?e:void 0,H=e=>{if(!s.isArrowFunction(e))return;let r=e.getBody();if(s.isParenthesizedExpression(r))r=r.getExpression();else if(s.isBlock(r)){const t=r.getStatements().find(n=>s.isReturnStatement(n));r=t&&s.isReturnStatement(t)?t.getExpression()??r:r}return s.isObjectLiteralExpression(r)?r:void 0},Q=e=>{if(!s.isPropertyAssignment(e))return;const r=e.getInitializer();if(!r||!s.isCallExpression(r))return;const t=r.getExpression();if(!s.isPropertyAccessExpression(t))return;const n=t.getName();if(n!=="one"&&n!=="many")return;const[i,o]=r.getArguments(),a=i&&s.isStringLiteral(i)?i.getLiteralText():"_unknown_";let l="_unknown_",c="_id",g;return o&&s.isObjectLiteralExpression(o)&&(l=d(o,"field")??l,c=d(o,"references")??c,g=n==="one"?G(d(o,"onDelete")):void 0),{field:l,kind:n,name:e.getName(),onDelete:g,references:c,table:a}},W=e=>{const r=H(e);if(!r)return[];const t=[];for(const n of r.getProperties()){const i=Q(n);i&&t.push(i)}return t},x=e=>e&&s.isStringLiteral(e)?e.getLiteralText():"_unnamed_",X=(e,r,t)=>{if(!r.includes("."))return;const[n="",...i]=r.split("."),o=`${n}${i.map(a=>a.charAt(0).toUpperCase()+a.slice(1)).join("")}`;throw u(e,`index "${t}" indexes the nested path ${JSON.stringify(r)} — Lunora indexes only top-level columns. Denormalise the value into its own column (e.g. \`${o}\`), index that instead, and keep it in sync with a table \`.triggers()\` beforeInsert/beforeUpdate.`)},Y=e=>{if(!e||!s.isObjectLiteralExpression(e))return!1;const r=e.getProperty("unique");if(!r||!s.isPropertyAssignment(r))return!1;const t=r.getInitializer();if(t===void 0)return!1;if(!s.isTrueLiteral(t)&&!s.isFalseLiteral(t))throw u(t,`\`unique\` must be a literal \`true\` or \`false\`, got ${JSON.stringify(t.getText())}`);return s.isTrueLiteral(t)},Z=e=>{const[r,t,n]=e,i=Y(n),o=x(r),a=t&&s.isArrayLiteralExpression(t)?t.getElements():[];for(const c of a)if(!s.isStringLiteral(c))throw u(c,`index "${o}" lists a non-literal field (${JSON.stringify(c.getText())}); codegen reads index fields statically, so every entry must be a string literal.`);const l=a.filter(c=>s.isStringLiteral(c));for(const c of l)X(c,c.getLiteralText(),o);return{fields:l.map(c=>c.getLiteralText()),name:o,unique:i}},ee=e=>{const[r,t]=e;let n="_unknown_",i,o,a,l;return t&&s.isObjectLiteralExpression(t)&&(n=d(t,"field")??n,i=h(t,"filterFields"),o=d(t,"language"),a=K(t,"staged"),l=d(t,"strategy")),{field:n,filterFields:i,language:o,name:x(r),staged:a,strategy:l}},te=e=>{const[r,t]=e;let n="_unknown_",i;return t&&s.isObjectLiteralExpression(t)&&(n=d(t,"field")??n,i=m(t,"precision")),{field:n,name:x(r),precision:i}},re=e=>{const[r,t]=e,n=r&&s.isStringLiteral(r)?r.getLiteralText():"_unknown_";let i;return t&&s.isObjectLiteralExpression(t)&&(i=m(t,"after")),{after:i,field:n}},ne=e=>{if(!s.isObjectLiteralExpression(e))return;const r=d(e,"field");return r===void 0?void 0:{direction:d(e,"direction")==="desc"?"desc":"asc",field:r}},ie=e=>{const[r,t]=e;let n=[],i;if(t&&s.isObjectLiteralExpression(t)){const o=t.getProperty("sortBy");if(o&&s.isPropertyAssignment(o)){const a=o.getInitializer();a&&s.isArrayLiteralExpression(a)&&(n=a.getElements().map(l=>ne(l)).filter(l=>l!==void 0))}i=h(t,"partitionBy")}return{name:x(r),partitionBy:i,sortBy:n}},se=(e,r)=>{const[t,n]=e,i=t&&s.isStringLiteral(t)?t.getLiteralText():"_unknown_";if(!(!n||!s.isObjectLiteralExpression(n)))return{dimensions:m(n,"dimensions"),field:i,metadata:h(n,"metadata"),metric:E(d(n,"metric")),name:d(n,"index")??"_unnamed_",table:r}},oe=e=>{const r=e[0];return r&&s.isObjectLiteralExpression(r)&&d(r,"backend")==="hyperdrive"?"hyperdrive":"d1"},ae=e=>{if(e&&s.isObjectLiteralExpression(e)){const r=e.getProperty("field");if(r&&s.isPropertyAssignment(r)){const t=r.getInitializer();if(t&&s.isStringLiteral(t))return t.getLiteralText()}}return"deletedAt"},p=(e,r)=>{const t=e.getProperty(r);if(t&&s.isPropertyAssignment(t)){const n=t.getInitializer();if(n&&s.isStringLiteral(n))return n.getLiteralText()}},le=(e,r)=>{const t=e.getProperty(r);if(t&&s.isPropertyAssignment(t)){const n=t.getInitializer();if(n&&s.isArrayLiteralExpression(n)){const i=n.getElements().filter(o=>s.isStringLiteral(o)).map(o=>o.getLiteralText());return i.length>0?i:void 0}}},ce=e=>{const r=e[0];return!r||!s.isObjectLiteralExpression(r)?{binding:"",hasTenantBy:!1,unanalyzable:!0}:{binding:p(r,"binding")??"",columns:le(r,"columns"),hasReconcile:r.getProperty("reconcileEveryMs")!==void 0,hasSoftDelete:r.getProperty("softDeleteColumn")!==void 0,hasTenantBy:r.getProperty("tenantBy")!==void 0,idColumn:p(r,"idColumn"),mode:p(r,"mode"),query:p(r,"query")}},de=(e,r,t,n)=>{switch(r){case"externallyManaged":{e.externallyManaged=!0;break}case"geoIndex":{e.geoIndexes.push(te(t));break}case"global":{e.shardMode="global",e.globalBackend=oe(t);break}case"index":{e.indexes.push(Z(t));break}case"public":{e.isPublic=!0;break}case"rankIndex":{e.rankIndexes.push(ie(t));break}case"relations":{const i=t[0];i&&e.relations.push(...W(i));break}case"searchIndex":{e.searchIndexes.push(ee(t));break}case"shardBy":{const i=t[0];e.shardMode={field:i&&s.isStringLiteral(i)?i.getLiteralText():"_unknown_",kind:"shardBy"};break}case"softDelete":{e.softDelete={field:ae(t[0])};break}case"source":{e.externalSource=ce(t),e.externallyManaged=!0;break}case"ttl":{e.ttl=re(t);break}case"vectorize":{const i=se(t,n);i&&e.vectorIndexes.push(i);break}}},L=(e,r)=>{const t={externallyManaged:!1,geoIndexes:[],indexes:[],isPublic:!1,rankIndexes:[],relations:[],searchIndexes:[],shardMode:"root",vectorIndexes:[]};let n={},i=e;for(;s.isCallExpression(i);){const o=i.getExpression(),a=i.getArguments();if(s.isPropertyAccessExpression(o))de(t,o.getName(),a,r),i=o.getExpression();else if(s.isIdentifier(o)&&o.getText()==="defineTable"){const l=a[0];if(l&&s.isObjectLiteralExpression(l))n=w(l);else if(l)throw u(l,`table "${r}" calls defineTable(${JSON.stringify(l.getText())}), but codegen reads the field map syntactically and can only read an object literal. Inline the fields into the defineTable(...) call. To reuse them in an \`.input()\`, derive from the generated \`Doc_${r}\` / \`Insert_${r}\` type instead of sharing the runtime value.`);break}else break}return t.softDelete&&!(t.softDelete.field in n)&&(n={...n,[t.softDelete.field]:{inner:{kind:"number"},kind:"optional"}}),{externallyManaged:t.externallyManaged,externalSource:t.externalSource,geoIndexes:t.geoIndexes,globalBackend:t.shardMode==="global"?t.globalBackend??"d1":void 0,indexes:t.indexes,isPublic:t.isPublic,name:r,rankIndexes:t.rankIndexes,relations:t.relations,searchIndexes:t.searchIndexes,shape:n,shardMode:t.shardMode,softDelete:t.softDelete,ttl:t.ttl,vectorIndexes:t.vectorIndexes}},ue=e=>{const r=e.getProperty("source");if(r&&s.isPropertyAssignment(r)){const t=r.getInitializer();if(t&&s.isObjectLiteralExpression(t))return d(t,"table")??"_unknown_"}return"_unknown_"},ge=e=>{if(!s.isPropertyAssignment(e))return;const r=e.getInitializer();if(!r||!s.isCallExpression(r))return;const t=r.getExpression();if(!s.isIdentifier(t)||t.getText()!=="defineVectorIndex")return;const n=r.getArguments()[0];if(!n||!s.isObjectLiteralExpression(n))return;const i=e.getNameNode(),o=s.isStringLiteral(i)?i.getLiteralText():i.getText();return{dimensions:m(n,"dimensions"),metric:E(d(n,"metric")),name:o,table:ue(n)}},P=e=>{const r=[];for(const t of e.getProperties()){const n=ge(t);n&&r.push(n)}return r},k=(e,r)=>`${e}_${r}`,y=(e,r,t)=>t.has(e)?k(r,e):e,fe=(e,r,t)=>{const n=k(r,e.name);return{...e,extensionKey:r,name:n,relations:e.relations.map(i=>({...i,table:y(i.table,r,t)})),vectorIndexes:e.vectorIndexes.map(i=>({...i,table:y(i.table,r,t)}))}},pe=e=>{if(s.isVariableDeclaration(e)||s.isPropertyAssignment(e))return e.getInitializer();if(s.isShorthandPropertyAssignment(e))return e.getNameNode()},me=(e,r)=>{const t=e.getProperty(r);if(t&&s.isPropertyAssignment(t))return t.getInitializer()},xe=e=>{if(!s.isCallExpression(e))return!1;const r=e.getExpression();return s.isIdentifier(r)&&r.getText()==="defineSchemaExtension"},be=e=>{if(s.isIdentifier(e))return e;if(s.isPropertyAccessExpression(e)){const r=e.getExpression();return s.isIdentifier(r)?r:void 0}},ye=e=>{if(s.isObjectLiteralExpression(e))return e;if(s.isCallExpression(e)){const r=e.getExpression();let t;if(s.isIdentifier(r)?t=r.getText():s.isPropertyAccessExpression(r)&&(t=r.getName()),t==="definePlugin"){const n=e.getArguments()[1];return n&&s.isObjectLiteralExpression(n)?n:void 0}}},he=(e,r)=>{const t=pe(e);if(t){if(s.isPropertyAccessExpression(r)){const n=ye(t);return n?me(n,r.getName()):void 0}return t}},ke=e=>{let r=e,t=0;for(;r&&t<32;){if(t+=1,xe(r))return r;const n=be(r),i=n&&s.isIdentifier(n)?n.getSymbol():void 0,o=(i?.getAliasedSymbol()?.getDeclarations()??i?.getDeclarations()??[])[0];if(!o)return;const a=o.getSourceFile();if(a.isInNodeModules()||a.isDeclarationFile())return;r=he(o,r)}},Ie=e=>{const[r,t]=e.getArguments();if(!(!r||!s.isStringLiteral(r))&&!(!t||!s.isObjectLiteralExpression(t)))return{key:r.getLiteralText(),options:t}},ve=e=>{const r=e.getProperty("tables");if(!r||!s.isPropertyAssignment(r))return[];const t=r.getInitializer();if(!t||!s.isObjectLiteralExpression(t))return[];const n=[];for(const i of t.getProperties()){if(!s.isPropertyAssignment(i))continue;const o=i.getInitializer();o&&n.push(L(o,i.getName()))}return n},Ee=e=>{const r=e.getProperty("vectorIndexes");if(!r||!s.isPropertyAssignment(r))return[];const t=r.getInitializer();return!t||!s.isObjectLiteralExpression(t)?[]:P(t)},S=(e,r,t)=>{const n=new Set(r.map(a=>a.name)),i=r.map(a=>fe(a,e,n)),o=t.map(a=>({...a,name:k(e,a.name),table:y(a.table,e,n)}));return{tables:i,vectorIndexes:o}},Le=(e,r)=>S(e,ve(r),Ee(r)),Pe=(e,r)=>{const t=e.getArguments()[0];if(!t)return;const n=ke(t);if(!n){if(r!==void 0){const o=V(t,r);if(o)return S(o.key,o.bareTables,o.bareVectorIndexes)}console.warn(`@lunora/codegen: skipping \`.extend(${t.getText()})\` — its \`defineSchemaExtension(...)\` could not be resolved from local sources, and ${r===void 0?"no project root was available to resolve the package":"the package could not be imported/introspected"}. Extension tables will be absent from the generated types.`);return}const i=Ie(n);if(!i){console.warn("@lunora/codegen: skipping `.extend(...)` — `defineSchemaExtension` requires a string `key` and an options object literal.");return}return Le(i.key,i.options)},Se=e=>{const r=[];let t=e;for(;;){const n=t.getParent();if(!n||!s.isPropertyAccessExpression(n))break;const i=n.getParent();if(!i||!s.isCallExpression(i))break;n.getName()==="extend"&&r.push(i),t=i}return r},A=(e,r,t,n,i)=>{let o=e;for(;;){const a=o.getParent();if(!a||!s.isPropertyAccessExpression(a))break;const l=a.getParent();if(!l||!s.isCallExpression(l))break;if(a.getName()===r){const c=l.getArguments()[0];if(!c||!s.isStringLiteral(c))throw u(l,`\`.${r}(...)\` expects a string literal (${i})`);const g=c.getLiteralText();if(!n.has(g))throw u(c,`unknown ${t} ${JSON.stringify(g)} — expected ${i}`);return g}o=l}},Ae=new Set(["eu","fedramp","us"]),Te=e=>A(e,"jurisdiction","jurisdiction",Ae,'"eu", "us", or "fedramp"'),je=new Set(["required"]),we=e=>A(e,"rls","rls mode",je,'"required"'),Ne=e=>{const r=[];for(const t of e.getProperties()){if(!s.isPropertyAssignment(t))continue;const n=t.getInitializer();if(n){const i=t.getName();U(i,t.getNameNode()),r.push(L(n,i))}}return r},Oe=(e,r,t)=>{const n=new Set(r.map(o=>o.name)),i=[];for(const o of Se(e)){const a=Pe(o,t);if(a){for(const l of a.tables){if(n.has(l.name))throw u(o,`defineSchema(...).extend(...): table "${l.name}" already exists — another extension with the same key already contributed it.`);n.add(l.name),r.push(l)}i.push(...a.vectorIndexes)}}return i},qe=(e,r,t)=>{const n=e.addSourceFileAtPath(r).getDescendantsOfKind(j.CallExpression).find(b=>{const I=b.getExpression();return s.isIdentifier(I)&&I.getText()==="defineSchema"});if(!n)throw new T("INTERNAL",`defineSchema() not found in ${r}`);const i=n.getArguments()[0];if(!i||!s.isObjectLiteralExpression(i))throw u(n,"defineSchema() expects an object literal");const o=Ne(i),a=n.getArguments()[1],l=a&&s.isObjectLiteralExpression(a)?P(a):[],c=Oe(n,o,t),g=[...o.flatMap(b=>b.vectorIndexes),...l,...c];return{jurisdiction:Te(n),rlsMode:we(n),tables:o,vectorIndexes:g}};export{qe as default};