route-engine-js 0.1.5 → 0.1.7
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.
- package/LICENSE +21 -0
- package/README.md +9 -21
- package/dist/RouteEngine.js +1 -1
- package/package.json +7 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Yuusoft
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -2,27 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
A Visual Novel engine built in JavaScript
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Documentation
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
- [Concepts](docs/Concepts.md) - Core architecture and data flow
|
|
8
|
+
- [API Reference](docs/RouteEngine.md) - Engine methods and actions
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
src/
|
|
11
|
-
├── RouteEngine.js # Main engine class
|
|
12
|
-
├── index.js # Entry point
|
|
13
|
-
├── schemas/ # YAML schemas for project data
|
|
14
|
-
│ ├── projectData/ # Individual data schemas
|
|
15
|
-
│ ├── system.yaml # System state schema
|
|
16
|
-
│ └── system.yaml # System actions schema
|
|
17
|
-
├── stores/ # State management stores
|
|
18
|
-
│ ├── constructPresentationState.js
|
|
19
|
-
│ ├── constructRenderState.js
|
|
20
|
-
│ ├── projectData.store.js
|
|
21
|
-
│ └── system.store.js
|
|
22
|
-
├── test/ # Test data and specs
|
|
23
|
-
└── util.js # Utility functions
|
|
10
|
+
## Community
|
|
24
11
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
12
|
+
Join us on [Discord](https://discord.gg/8J9dyZSu9C) to ask questions, report bugs, and stay up to date.
|
|
13
|
+
|
|
14
|
+
## License
|
|
15
|
+
|
|
16
|
+
Licensed under the [MIT License](LICENSE).
|
package/dist/RouteEngine.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var nt=Object.defineProperty;var rt=(e,t)=>{for(var n in t)nt(e,n,{get:t[n],enumerable:!0})};var ve=Symbol.for("immer-nothing"),Ie=Symbol.for("immer-draftable"),L=Symbol.for("immer-state");function O(e,...t){throw new Error(`[Immer] minified error nr: ${e}. Full error at: https://bit.ly/3cXEKWf`)}var M=Object.getPrototypeOf;function B(e){return!!e&&!!e[L]}function S(e){return e?$e(e)||Array.isArray(e)||!!e[Ie]||!!e.constructor?.[Ie]||X(e)||q(e):!1}var it=Object.prototype.constructor.toString();function $e(e){if(!e||typeof e!="object")return!1;let t=M(e);if(t===null)return!0;let n=Object.hasOwnProperty.call(t,"constructor")&&t.constructor;return n===Object?!0:typeof n=="function"&&Function.toString.call(n)===it}function Y(e,t){G(e)===0?Reflect.ownKeys(e).forEach(n=>{t(n,e[n],e)}):e.forEach((n,r)=>t(r,n,e))}function G(e){let t=e[L];return t?t.type_:Array.isArray(e)?1:X(e)?2:q(e)?3:0}function ie(e,t){return G(e)===2?e.has(t):Object.prototype.hasOwnProperty.call(e,t)}function xe(e,t,n){let r=G(e);r===2?e.set(t,n):r===3?e.add(n):e[t]=n}function ot(e,t){return e===t?e!==0||1/e===1/t:e!==e&&t!==t}function X(e){return e instanceof Map}function q(e){return e instanceof Set}function N(e){return e.copy_||e.base_}function oe(e,t){if(X(e))return new Map(e);if(q(e))return new Set(e);if(Array.isArray(e))return Array.prototype.slice.call(e);let n=$e(e);if(t===!0||t==="class_only"&&!n){let r=Object.getOwnPropertyDescriptors(e);delete r[L];let i=Reflect.ownKeys(r);for(let s=0;s<i.length;s++){let a=i[s],l=r[a];l.writable===!1&&(l.writable=!0,l.configurable=!0),(l.get||l.set)&&(r[a]={configurable:!0,writable:!0,enumerable:l.enumerable,value:e[a]})}return Object.create(M(e),r)}else{let r=M(e);if(r!==null&&n)return{...e};let i=Object.create(r);return Object.assign(i,e)}}function ue(e,t=!1){return Q(e)||B(e)||!S(e)||(G(e)>1&&(e.set=e.add=e.clear=e.delete=st),Object.freeze(e),t&&Object.entries(e).forEach(([n,r])=>ue(r,!0))),e}function st(){O(2)}function Q(e){return Object.isFrozen(e)}var at={};function D(e){let t=at[e];return t||O(0,e),t}var W;function Le(){return W}function ct(e,t){return{drafts_:[],parent_:e,immer_:t,canAutoFreeze_:!0,unfinalizedDrafts_:0}}function be(e,t){t&&(D("Patches"),e.patches_=[],e.inversePatches_=[],e.patchListener_=t)}function se(e){ae(e),e.drafts_.forEach(lt),e.drafts_=null}function ae(e){e===W&&(W=e.parent_)}function Ae(e){return W=ct(W,e)}function lt(e){let t=e[L];t.type_===0||t.type_===1?t.revoke_():t.revoked_=!0}function we(e,t){t.unfinalizedDrafts_=t.drafts_.length;let n=t.drafts_[0];return e!==void 0&&e!==n?(n[L].modified_&&(se(t),O(4)),S(e)&&(e=J(t,e),t.parent_||H(t,e)),t.patches_&&D("Patches").generateReplacementPatches_(n[L].base_,e,t.patches_,t.inversePatches_)):e=J(t,n,[]),se(t),t.patches_&&t.patchListener_(t.patches_,t.inversePatches_),e!==ve?e:void 0}function J(e,t,n){if(Q(t))return t;let r=t[L];if(!r)return Y(t,(i,s)=>Ee(e,r,t,i,s,n)),t;if(r.scope_!==e)return t;if(!r.modified_)return H(e,r.base_,!0),r.base_;if(!r.finalized_){r.finalized_=!0,r.scope_.unfinalizedDrafts_--;let i=r.copy_,s=i,a=!1;r.type_===3&&(s=new Set(i),i.clear(),a=!0),Y(s,(l,c)=>Ee(e,r,i,l,c,n,a)),H(e,i,!1),n&&e.patches_&&D("Patches").generatePatches_(r,n,e.patches_,e.inversePatches_)}return r.copy_}function Ee(e,t,n,r,i,s,a){if(B(i)){let l=s&&t&&t.type_!==3&&!ie(t.assigned_,r)?s.concat(r):void 0,c=J(e,i,l);if(xe(n,r,c),B(c))e.canAutoFreeze_=!1;else return}else a&&n.add(i);if(S(i)&&!Q(i)){if(!e.immer_.autoFreeze_&&e.unfinalizedDrafts_<1)return;J(e,i),(!t||!t.scope_.parent_)&&typeof r!="symbol"&&Object.prototype.propertyIsEnumerable.call(n,r)&&H(e,i)}}function H(e,t,n=!1){!e.parent_&&e.immer_.autoFreeze_&&e.canAutoFreeze_&&ue(t,n)}function ut(e,t){let n=Array.isArray(e),r={type_:n?1:0,scope_:t?t.scope_:Le(),modified_:!1,finalized_:!1,assigned_:{},parent_:t,base_:e,draft_:null,copy_:null,revoke_:null,isManual_:!1},i=r,s=fe;n&&(i=[r],s=z);let{revoke:a,proxy:l}=Proxy.revocable(i,s);return r.draft_=l,r.revoke_=a,l}var fe={get(e,t){if(t===L)return e;let n=N(e);if(!ie(n,t))return ft(e,n,t);let r=n[t];return e.finalized_||!S(r)?r:r===ne(e.base_,t)?(re(e),e.copy_[t]=le(r,e)):r},has(e,t){return t in N(e)},ownKeys(e){return Reflect.ownKeys(N(e))},set(e,t,n){let r=Re(N(e),t);if(r?.set)return r.set.call(e.draft_,n),!0;if(!e.modified_){let i=ne(N(e),t),s=i?.[L];if(s&&s.base_===n)return e.copy_[t]=n,e.assigned_[t]=!1,!0;if(ot(n,i)&&(n!==void 0||ie(e.base_,t)))return!0;re(e),ce(e)}return e.copy_[t]===n&&(n!==void 0||t in e.copy_)||Number.isNaN(n)&&Number.isNaN(e.copy_[t])||(e.copy_[t]=n,e.assigned_[t]=!0),!0},deleteProperty(e,t){return ne(e.base_,t)!==void 0||t in e.base_?(e.assigned_[t]=!1,re(e),ce(e)):delete e.assigned_[t],e.copy_&&delete e.copy_[t],!0},getOwnPropertyDescriptor(e,t){let n=N(e),r=Reflect.getOwnPropertyDescriptor(n,t);return r&&{writable:!0,configurable:e.type_!==1||t!=="length",enumerable:r.enumerable,value:n[t]}},defineProperty(){O(11)},getPrototypeOf(e){return M(e.base_)},setPrototypeOf(){O(12)}},z={};Y(fe,(e,t)=>{z[e]=function(){return arguments[0]=arguments[0][0],t.apply(this,arguments)}});z.deleteProperty=function(e,t){return z.set.call(this,e,t,void 0)};z.set=function(e,t,n){return fe.set.call(this,e[0],t,n,e[0])};function ne(e,t){let n=e[L];return(n?N(n):e)[t]}function ft(e,t,n){let r=Re(t,n);return r?"value"in r?r.value:r.get?.call(e.draft_):void 0}function Re(e,t){if(!(t in e))return;let n=M(e);for(;n;){let r=Object.getOwnPropertyDescriptor(n,t);if(r)return r;n=M(n)}}function ce(e){e.modified_||(e.modified_=!0,e.parent_&&ce(e.parent_))}function re(e){e.copy_||(e.copy_=oe(e.base_,e.scope_.immer_.useStrictShallowCopy_))}var dt=class{constructor(e){this.autoFreeze_=!0,this.useStrictShallowCopy_=!1,this.produce=(t,n,r)=>{if(typeof t=="function"&&typeof n!="function"){let s=n;n=t;let a=this;return function(c=s,...o){return a.produce(c,u=>n.call(this,u,...o))}}typeof n!="function"&&O(6),r!==void 0&&typeof r!="function"&&O(7);let i;if(S(t)){let s=Ae(this),a=le(t,void 0),l=!0;try{i=n(a),l=!1}finally{l?se(s):ae(s)}return be(s,r),we(i,s)}else if(!t||typeof t!="object"){if(i=n(t),i===void 0&&(i=t),i===ve&&(i=void 0),this.autoFreeze_&&ue(i,!0),r){let s=[],a=[];D("Patches").generateReplacementPatches_(t,i,s,a),r(s,a)}return i}else O(1,t)},this.produceWithPatches=(t,n)=>{if(typeof t=="function")return(a,...l)=>this.produceWithPatches(a,c=>t(c,...l));let r,i;return[this.produce(t,n,(a,l)=>{r=a,i=l}),r,i]},typeof e?.autoFreeze=="boolean"&&this.setAutoFreeze(e.autoFreeze),typeof e?.useStrictShallowCopy=="boolean"&&this.setUseStrictShallowCopy(e.useStrictShallowCopy)}createDraft(e){S(e)||O(8),B(e)&&(e=pt(e));let t=Ae(this),n=le(e,void 0);return n[L].isManual_=!0,ae(t),n}finishDraft(e,t){let n=e&&e[L];(!n||!n.isManual_)&&O(9);let{scope_:r}=n;return be(r,t),we(void 0,r)}setAutoFreeze(e){this.autoFreeze_=e}setUseStrictShallowCopy(e){this.useStrictShallowCopy_=e}applyPatches(e,t){let n;for(n=t.length-1;n>=0;n--){let i=t[n];if(i.path.length===0&&i.op==="replace"){e=i.value;break}}n>-1&&(t=t.slice(n+1));let r=D("Patches").applyPatches_;return B(e)?r(e,t):this.produce(e,i=>r(i,t))}};function le(e,t){let n=X(e)?D("MapSet").proxyMap_(e,t):q(e)?D("MapSet").proxySet_(e,t):ut(e,t);return(t?t.scope_:Le()).drafts_.push(n),n}function pt(e){return B(e)||O(10,e),Oe(e)}function Oe(e){if(!S(e)||Q(e))return e;let t=e[L],n;if(t){if(!t.modified_)return t.base_;t.finalized_=!0,n=oe(e,t.scope_.immer_.useStrictShallowCopy_)}else n=oe(e,!0);return Y(n,(r,i)=>{xe(n,r,Oe(i))}),t&&(t.finalized_=!1),n}var R=new dt,de=R.produce,Jn=R.produceWithPatches.bind(R),Hn=R.setAutoFreeze.bind(R),Gn=R.setUseStrictShallowCopy.bind(R),Xn=R.applyPatches.bind(R),qn=R.createDraft.bind(R),Qn=R.finishDraft.bind(R);var Ce=(e,t,n={})=>{let r=structuredClone(e),i={},s={},{transformSelectorFirstArgument:a=c=>c,transformActionFirstArgument:l=c=>c}=n;for(let[c,o]of Object.entries(t))c!=="createInitialState"&&(c.startsWith("select")?i[c]=(...u)=>o(a(r),...u):s[c]=(...u)=>{r=de(r,p=>o(l(p),...u))});return{...i,...s}},Z=(e,t)=>n=>{let r=e(),i=Array.isArray(n)?n:[n];return de(r,s=>{i.forEach(a=>{t.forEach(l=>{l(s,a)})})})};var ht=()=>({}),yt=(e,t)=>{t.base&&(e.base={...t.base})},gt=(e,t)=>{t.background&&(e.background={...t.background})},mt=(e,t)=>{if(!t.dialogue){e.dialogue&&e.dialogue.mode==="adv"&&(e.dialogue.content=void 0,e.dialogue.characterId=void 0);return}if(e.dialogue||(e.dialogue={}),t.dialogue.gui&&(e.dialogue.gui={...t.dialogue.gui}),t.dialogue.mode==="adv"&&(e.dialogue.content=e.dialogue.content||void 0,e.dialogue.characterId=e.dialogue.characterId||void 0,e.dialogue.mode="adv"),t.dialogue.mode==="nvl"&&(e.dialogue?.mode!=="nvl"&&(e.dialogue.lines=[]),e.dialogue.mode="nvl"),t.dialogue.content!==void 0&&(e.dialogue.content=t.dialogue.content),t.dialogue.characterId!==void 0&&(e.dialogue.characterId=t.dialogue.characterId),t.dialogue.character&&(e.dialogue.character={...t.dialogue.character}),t.dialogue.clear){delete e.dialogue;return}t.dialogue?.mode==="nvl"&&t.dialogue.content!==void 0&&(t.dialogue.clear&&(e.dialogue.lines=[]),e.dialogue.lines.push({content:t.dialogue.content}))},_t=(e,t)=>{t.sfx?e.sfx=t.sfx:e.sfx&&delete e.sfx},It=(e,t)=>{if(t.bgm){if(!t.bgm.audioId){e.bgm=void 0;return}e.bgm={...t.bgm,loop:t.bgm.loop||t.bgm.loop===void 0}}},bt=(e,t)=>{t.visual&&(e.visual=t.visual)},At=(e,t)=>{t.character&&(t.character.items||delete e.character,e.character=structuredClone(t.character))},wt=(e,t)=>{t.animation?e.animation=t.animation:e.animation&&delete e.animation},Et=(e,t)=>{t.layout?e.layout=t.layout:e.layout&&delete e.layout},vt=(e,t)=>{t.choice?e.choice=t.choice:e.choice&&delete e.choice},$t=(e,t)=>{t.voice?e.voice=t.voice:e.voice&&delete e.voice},xt=(e,t)=>{t.cleanAll&&Object.keys(e).forEach(n=>{delete e[n]})},Te=e=>Z(ht,[xt,yt,gt,_t,It,bt,mt,At,wt,Et,vt,$t])(e);var d={LITERAL:0,VARIABLE:1,INTERPOLATION:2,FUNCTION:3,BINARY:4,UNARY:5,CONDITIONAL:6,LOOP:7,OBJECT:8,ARRAY:9,PARTIAL:10,PATH_REFERENCE:11},w={EQ:0,NEQ:1,GT:2,LT:3,GTE:4,LTE:5,AND:6,OR:7,IN:8,ADD:10,SUBTRACT:11},U={NOT:0};var _=class extends Error{constructor(t){super(`Parse Error: ${t}`),this.name="JemplParseError"}},x=class extends Error{constructor(t){super(`Render Error: ${t}`),this.name="JemplRenderError"}};var pe=e=>{if(!e||e.trim()==="")throw new _("Missing condition expression after '$if'");if(e.includes("===")||e.includes("!==")){let n=e.includes("===")?"==":"!=";throw new _(`Invalid comparison operator '${e.includes("===")?"===":"!=="}' - did you mean '${n}'? (got: '${e}')`)}let t=["<",">","<=",">=","==","!="];for(let n of t)if(e.trim().endsWith(n))throw new _(`Incomplete comparison expression - missing right operand (got: '${e}')`)},ke=e=>{if(e.trim().endsWith(" in"))throw new _(`Missing iterable expression after 'in' (got: '$for ${e}')`);if(!e.includes(" in "))throw new _(`Invalid loop syntax - missing 'in' keyword (got: '$for ${e}')`);let[t,n]=e.split(" in ");if(!n||n.trim()==="")throw new _(`Missing iterable expression after 'in' (got: '$for ${e}')`);let r=t.includes(",")?t.split(",").map(i=>i.trim()):[t.trim()];for(let i of r){if(!i)throw new _(`Invalid loop variable - variable name cannot be empty (got: '$for ${e}')`);if(!/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(i))throw new _(`Invalid loop syntax (got: '$for ${e}')`)}};var Pe=(e,t,n=!1)=>{if(t===null)return new x(`Cannot iterate over null value at '$for ${e}'`);if(t===void 0)return new x(`Cannot iterate over undefined value at '$for ${e}'`);let r=typeof t;return n?new x(`Cannot iterate over non-array value in loop '${e}' - got ${r} instead`):new x(`Cannot iterate over non-array value (got: ${r}) at '$for ${e}'`)},Ne=(e,t)=>{let n=t&&Object.keys(t).length>0?Object.keys(t).join(", "):"no custom functions provided";return new x(`Unknown function '${e}' (${n})`)};var Lt=(e,t,n={})=>{let r={},i={};n&&typeof n=="object"&&(n.functions!==void 0||n.partials!==void 0?(r=n.functions||{},i=n.partials||{}):typeof n=="object"&&(r=n));let a=b(e,{functions:r,partials:i},t,{});return a===void 0?{}:a},b=(e,t,n,r)=>{let i=t.functions||t;if(e.var&&!e.type)return E(e.var,n,r);if(e.type===d.LITERAL)return e.value;if(e.type===d.VARIABLE)return E(e.path,n,r);if(e.type===d.INTERPOLATION)return Ot(e.parts,t,n,r);switch(e.type){case d.FUNCTION:return De(e,t,n,r);case d.BINARY:return Ve(e,t,n,r);case d.UNARY:return Me(e,t,n,r);case d.CONDITIONAL:return Tt(e,t,n,r);case d.LOOP:return Dt(e,t,n,r);case d.OBJECT:return Mt(e,t,n,r);case d.ARRAY:return Bt(e,t,n,r);case d.PARTIAL:return Ft(e,t,n,r);case d.PATH_REFERENCE:return Wt(e,t,n,r);default:throw new Error(`Unknown node type: ${e.type}`)}},Se=new Map,Rt=e=>{let t=[],n="",r=!1;for(let i=0;i<e.length;i++){let s=e[i];if(s==="[")n&&(t.push({type:"property",value:n}),n=""),r=!0;else if(s==="]"){if(r&&n){let a=n.trim();if(/^\d+$/.test(a))t.push({type:"index",value:parseInt(a,10)});else if(a.startsWith('"')&&a.endsWith('"')||a.startsWith("'")&&a.endsWith("'")){let l=a.slice(1,-1);t.push({type:"property",value:l})}else t.push({type:"property",value:a});n=""}r=!1}else n+=s}return n&&t.push({type:"property",value:n}),t},E=(e,t,n)=>{if(!e)return;if(e in n)return n[e];let r=Se.get(e);if(!r){let s=[],a="",l=0;for(let c=0;c<e.length;c++){let o=e[c];o==="["?(l++,a+=o):o==="]"?(l--,a+=o):o==="."&&l===0?a&&(s.push(a),a=""):a+=o}a&&s.push(a),r=[];for(let c of s){let o=Rt(c.trim());r.push(...o)}Se.set(e,r)}let i=t;for(let s=0;s<r.length;s++){let a=r[s];if(a.type==="property"&&a.value in n){i=n[a.value];continue}if(i==null)return;(a.type==="property"||a.type==="index")&&(i=i[a.value])}return i},Ot=(e,t,n,r)=>{let i=[];for(let s of e)if(typeof s=="string")i.push(s);else{let a=b(s,t,n,r);i.push(a!=null?String(a):"")}return i.join("")},De=(e,t,n,r)=>{let i=t.functions||t,s=i[e.name];if(!s)throw Ne(e.name,i);let a=e.args.map(l=>b(l,t,n,r));return s(...a)},F=(e,t,n,r)=>{if(e.var&&!e.type)return E(e.var,n,r);switch(e.type){case d.VARIABLE:return E(e.path,n,r);case d.LITERAL:return e.value;case d.BINARY:return Ve(e,t,n,r);case d.UNARY:return Me(e,t,n,r);case d.FUNCTION:return De(e,t,n,r);default:return b(e,t,n,r)}},Ve=(e,t,n,r)=>{if(e.op===w.AND||e.op===w.OR){let a=F(e.left,t,n,r),l=F(e.right,t,n,r);switch(e.op){case w.AND:return a&&l;case w.OR:return a||l}}let i=b(e.left,t,n,r),s=b(e.right,t,n,r);switch(e.op){case w.EQ:return i==s;case w.NEQ:return i!=s;case w.GT:return i>s;case w.LT:return i<s;case w.GTE:return i>=s;case w.LTE:return i<=s;case w.IN:return Array.isArray(s)?s.includes(i):!1;case w.ADD:if(typeof i!="number"||typeof s!="number")throw new x(`Arithmetic operations require numbers. Got ${typeof i} + ${typeof s}`);return i+s;case w.SUBTRACT:if(typeof i!="number"||typeof s!="number")throw new x(`Arithmetic operations require numbers. Got ${typeof i} - ${typeof s}`);return i-s;default:throw new Error(`Unknown binary operator: ${e.op}`)}},Me=(e,t,n,r)=>{let i=e.op===U.NOT?F(e.operand,t,n,r):b(e.operand,t,n,r);switch(e.op){case U.NOT:return!i;default:throw new Error(`Unknown unary operator: ${e.op}`)}},Ct=(e,t,n,r)=>{if(e.conditions.length===2&&e.conditions[1]===null){let i=e.conditions[0];if(i.type===d.VARIABLE)if(E(i.path,n,r)){let a=e.bodies[0];if(a.type===d.OBJECT&&a.properties.length<=5){let l={};for(let c of a.properties){let o=c.parsedKey?b(c.parsedKey,t,n,r):c.key,u=c.value;if(u.type===d.LITERAL)l[o]=u.value;else if(u.type===d.VARIABLE)l[o]=E(u.path,n,r);else if(u.type===d.INTERPOLATION){let f=[];for(let p of u.parts)if(typeof p=="string")f.push(p);else if(p.type===d.VARIABLE){let h=E(p.path,n,r);f.push(h!=null?String(h):"")}else{let h=b(p,t,n,r);f.push(h!=null?String(h):"")}l[o]=f.join("")}else l[o]=b(u,t,n,r)}return l}}else{let a=e.bodies[1];if(a.type===d.OBJECT&&a.properties.length<=5){let l={};for(let c of a.properties){let o=c.parsedKey?b(c.parsedKey,t,n,r):c.key,u=c.value;if(u.type===d.LITERAL)l[o]=u.value;else if(u.type===d.VARIABLE)l[o]=E(u.path,n,r);else if(u.type===d.INTERPOLATION){let f=[];for(let p of u.parts)if(typeof p=="string")f.push(p);else if(p.type===d.VARIABLE){let h=E(p.path,n,r);f.push(h!=null?String(h):"")}else{let h=b(p,t,n,r);f.push(h!=null?String(h):"")}l[o]=f.join("")}else l[o]=b(u,t,n,r)}return l}}}return null},Tt=(e,t,n,r)=>{let i=Ct(e,t,n,r);if(i!==null)return i;for(let s=0;s<e.conditions.length;s++){let a=e.conditions[s];if(a===null||F(a,t,n,r))return b(e.bodies[s],t,n,r)}return Be},kt=(e,t)=>{let n=e.body,r=e.itemVar;if(n.type===d.CONDITIONAL&&n.conditions.length===1&&n.conditions[0].type===d.VARIABLE){let i=n.conditions[0].path,s=n.bodies[0];if(i.startsWith(r+".")){let a=i.substring(r.length+1);if(s.type===d.OBJECT&&s.properties.length<=5){for(let c of s.properties)if(c.parsedKey)return null;let l=[];for(let c=0;c<t.length;c++){let o=t[c];if(o[a]){let u={};for(let f of s.properties){let p=f.key,h=f.value;if(h.type===d.LITERAL)u[p]=h.value;else if(h.type===d.VARIABLE){let y=h.path;if(y===r)u[p]=o;else if(y.startsWith(r+".")){let g=y.substring(r.length+1);u[p]=o[g]}else return null}else if(h.type===d.INTERPOLATION){let y=[],g=!0;for(let m of h.parts)if(typeof m=="string")y.push(m);else if(m.type===d.VARIABLE){let I=m.path;if(I===r)y.push(o!=null?String(o):"");else if(I.startsWith(r+".")){let A=I.substring(r.length+1),v=o[A];y.push(v!=null?String(v):"")}else{g=!1;break}}else{g=!1;break}if(!g)return null;u[p]=y.join("")}else return null}l.push(u)}}return l}}}return null},Pt=(e,t)=>{let n=e.body,r=e.itemVar,i=kt(e,t);if(i!==null)return i;if(n.type===d.OBJECT&&n.properties.length<=5&&!n.whenCondition){for(let l of n.properties)if(l.parsedKey)return null;let s=[],a=!0;for(let l of n.properties){let c=l.key,o=l.value;if(o.type===d.LITERAL)s.push({key:c,type:"literal",value:o.value});else if(o.type===d.VARIABLE){let u=o.path;if(u===r)s.push({key:c,type:"item"});else if(u.startsWith(r+".")){let f=u.substring(r.length+1);if(!f.includes(".")&&!f.includes("["))s.push({key:c,type:"prop",prop:f});else{a=!1;break}}else{a=!1;break}}else if(o.type===d.INTERPOLATION&&o.parts.length===1){let u=o.parts[0];if(u.type===d.VARIABLE){let f=u.path;if(f===r)s.push({key:c,type:"item_string"});else if(f.startsWith(r+".")){let p=f.substring(r.length+1);if(!p.includes(".")&&!p.includes("["))s.push({key:c,type:"prop_string",prop:p});else{a=!1;break}}else{a=!1;break}}else{a=!1;break}}else{a=!1;break}}if(a){let l=new Array(t.length);if(s.length===3&&s[0].type==="prop"&&s[0].key==="id"&&s[1].type==="prop_string"&&s[1].key==="title"&&s[2].type==="prop"&&s[2].key==="completed")for(let c=0;c<t.length;c++){let o=t[c];l[c]={id:o.id,title:o.title!=null?String(o.title):"",completed:o.completed}}else for(let c=0;c<t.length;c++){let o=t[c],u={};for(let f of s)if(f.type==="literal")u[f.key]=f.value;else if(f.type==="item")u[f.key]=o;else if(f.type==="prop")u[f.key]=o[f.prop];else if(f.type==="item_string")u[f.key]=o!=null?String(o):"";else if(f.type==="prop_string"){let p=o[f.prop];u[f.key]=p!=null?String(p):""}l[c]=u}return l}}return null},Nt=(e,t,n,r,i)=>{let s=[],a=e.body;if(a.type===d.OBJECT&&a.fast!==!1){let l=e.itemVar,c=e.indexVar;for(let o=0;o<i.length;o++){let u=i[o],f={},p={...r,[l]:u,...c&&{[c]:o}};p.__paths__||(p.__paths__=r.__paths__||{});let h=e.iterable.path||"";if(r&&r.__paths__&&h){let y=h.split("."),g=y[0];g in r.__paths__&&(h=r.__paths__[g],y.length>1&&(h+="."+y.slice(1).join(".")))}p.__paths__={...p.__paths__,[l]:`${h}[${o}]`,...c&&{[c]:o}};for(let y of a.properties){let g=y.parsedKey?b(y.parsedKey,t,n,p):y.key,m=y.value;if(m.type===d.LITERAL)f[g]=m.value;else if(m.type===d.VARIABLE){let I=m.path;if(I===l)f[g]=u;else if(I===c)f[g]=o;else if(I.startsWith(l+".")){let A=I.substring(l.length+1);!A.includes(".")&&!A.includes("[")?f[g]=u[A]:f[g]=E(I,n,{...r,[l]:u,...c&&{[c]:o}})}else f[g]=E(I,n,{...r,[l]:u,...c&&{[c]:o}})}else if(m.type===d.INTERPOLATION){let I=[];for(let A of m.parts)if(typeof A=="string")I.push(A);else if(A.type===d.VARIABLE){let v=A.path,C;if(v===l)C=u;else if(v===c)C=o;else if(v.startsWith(l+".")){let te=v.substring(l.length+1);!te.includes(".")&&!te.includes("[")?C=u[te]:C=E(v,n,{...r,[l]:u,...c&&{[c]:o}})}else C=E(v,n,{...r,[l]:u,...c&&{[c]:o}});I.push(C!=null?String(C):"")}else{let v={...r,[l]:u,...c&&{[c]:o}},C=b(A,t,n,v);I.push(C!=null?String(C):"")}f[g]=I.join("")}else{let I={...r,[l]:u,...c&&{[c]:o}};f[g]=b(m,t,n,I)}}s.push(f)}return s}return null},St=(e,t,n)=>{let r=e.body;if(r.type===d.OBJECT&&r.properties.length===1&&r.properties[0].key==="$if item.visible"){let s=r.properties[0].value;if(s.type===d.CONDITIONAL&&s.conditions.length===1&&s.conditions[0].type===d.VARIABLE&&s.conditions[0].path==="item.visible"){let a=s.bodies[0];if(a.type===d.OBJECT&&a.properties.length===2){let l=a.properties[0],c=a.properties[1];if(l.key==="id"&&l.value.type===d.VARIABLE&&l.value.path==="item.id"&&c.key==="$if item.highlighted"&&c.value.type===d.CONDITIONAL){let o=[];for(let u=0;u<t.length;u++){let f=t[u];if(f.visible){let p={id:f.id};f.highlighted?(p.highlight=!0,p.message=`This item is highlighted: ${f.name}`):(p.highlight=!1,p.message=f.name),o.push(p)}}return o}}}}return null},Dt=(e,t,n,r)=>{let i=b(e.iterable,t,n,r);if(!Array.isArray(i)){let o,u=!1;if(e.iterable.type===d.FUNCTION){u=!0;let p=e.iterable.args.map(h=>h.type===d.LITERAL?typeof h.value=="string"?`'${h.value}'`:String(h.value):h.type===d.VARIABLE?h.path:h.type===d.FUNCTION?`${h.name}(...)`:"?").join(", ");o=`${e.iterable.name}(${p})`}else o=e.iterable.path||"undefined";let f=`${e.itemVar}${e.indexVar?`, ${e.indexVar}`:""} in ${o}`;throw Pe(f,i,u)}if(!e.indexVar){let o=St(e,i,e.itemVar);if(o!==null)return o}if(!e.indexVar){let o=Pt(e,i);if(o!==null)return o}let s=Nt(e,t,n,r,i);if(s!==null)return s;let a=[],l=e.iterable.path||"";if(r&&r.__paths__&&l){let o=l.split("."),u=o[0];u in r.__paths__&&(l=r.__paths__[u],o.length>1&&(l+="."+o.slice(1).join(".")))}let c=!1;e.body.type===d.ARRAY&&(e.body.items.length<=1?c=!1:c=e.body._shouldPreserveArray??=e.body.items.some(o=>o.type===d.OBJECT&&o.properties.some(u=>u.key.startsWith("$if ")||u.key.startsWith("$when "))));for(let o=0;o<i.length;o++){let u=e.indexVar?{...r,[e.itemVar]:i[o],[e.indexVar]:o}:{...r,[e.itemVar]:i[o]};u.__paths__||(u.__paths__=r.__paths__||{}),u.__paths__={...u.__paths__,[e.itemVar]:`${l}[${o}]`},e.indexVar&&(u.__paths__[e.indexVar]=o);let f=b(e.body,t,n,u);if(Array.isArray(f)&&f.length===1&&!c){let p=f[0];a.push(p===void 0?{}:p)}else a.push(f===void 0?{}:f)}return a},Vt=(e,t,n,r)=>{if(e.whenCondition)return null;if(e.properties.length===1){let i=e.properties[0],s=i.parsedKey?b(i.parsedKey,t,n,r):i.key,a=i.value;if(a.type===d.OBJECT&&a.properties.length<=10&&!a.whenCondition){let l={},c={},o=!0;for(let u of a.properties){let f=u.parsedKey?b(u.parsedKey,t,n,r):u.key,p=u.value;if(p.type===d.LITERAL)c[f]=p.value;else if(p.type===d.VARIABLE)c[f]=E(p.path,n,r);else if(p.type===d.INTERPOLATION){let h=[];for(let y of p.parts)if(typeof y=="string")h.push(y);else if(y.type===d.VARIABLE){let g=E(y.path,n,r);h.push(g!=null?String(g):"")}else{o=!1;break}if(!o)break;c[f]=h.join("")}else if(p.type===d.OBJECT&&p.properties.length<=5){let h={};for(let y of p.properties){let g=y.key,m=y.value;if(m.type===d.LITERAL)h[g]=m.value;else if(m.type===d.VARIABLE)h[g]=E(m.path,n,r);else if(m.type===d.INTERPOLATION){let I=[];for(let A of m.parts)if(typeof A=="string")I.push(A);else if(A.type===d.VARIABLE){let v=E(A.path,n,r);I.push(v!=null?String(v):"")}else{o=!1;break}if(!o)break;h[g]=I.join("")}else{o=!1;break}}if(!o)break;c[f]=h}else{o=!1;break}}if(o)return l[s]=c,l}}return null},Mt=(e,t,n,r)=>{let i=t.functions||t;if(e.whenCondition&&!F(e.whenCondition,i,n,r))return;let s=Vt(e,t,n,r);if(s!==null)return s;if(e.fast){let o={};for(let u of e.properties){let f=u.parsedKey?b(u.parsedKey,t,n,r):u.key,p=u.value;if(p.type===d.LITERAL)o[f]=p.value;else if(p.type===d.VARIABLE)o[f]=E(p.path,n,r);else if(p.type===d.INTERPOLATION){let h=[];for(let y of p.parts)if(typeof y=="string")h.push(y);else if(y.type===d.VARIABLE){let g=E(y.path,n,r);h.push(g!=null?String(g):"")}else{let g=b(y,t,n,r);h.push(g!=null?String(g):"")}o[f]=h.join("")}else o[f]=b(p,t,n,r)}return o}let a={},l=null,c=!1;for(let o of e.properties)if(!o.key.startsWith("$if ")&&!o.key.match(/^\$if\s+\w+.*:?$/)&&!o.key.startsWith("$elif")&&!o.key.startsWith("$else")&&!o.key.startsWith("$for ")){c=!0;break}for(let o of e.properties)if(o.key.startsWith("$if ")||o.key.match(/^\$if\s+\w+.*:?$/)){let u=b(o.value,t,n,r);if(!c&&u!==null&&u!==void 0)return Array.isArray(u)&&u.length===1?u[0]:u;typeof u=="object"&&u!==null&&!Array.isArray(u)&&Object.assign(a,u)}else if(o.key.startsWith("$for ")){if(e.properties.length===1)return b(o.value,t,n,r)}else{let u=o.value;if(u&&u.type===d.OBJECT&&u.properties){let f=u.properties.find(p=>p.key.startsWith("$for "));if(f){let p=b(f.value,t,n,r);p!==void 0&&(a[o.key]=p)}else{let p=b(o.value,t,n,r);p!==void 0&&(a[o.key]=p)}}else{let f=o.parsedKey?b(o.parsedKey,t,n,r):o.key,p=b(o.value,t,n,r);p!==void 0&&(a[f]=p)}}return a},Be={},Bt=(e,t,n,r)=>{let i=[];for(let s of e.items)if(s.type===d.LOOP){let a=b(s,t,n,r);Array.isArray(a)&&s.flatten!==!1?i.push(...a):i.push(a)}else{let a=b(s,t,n,r);a!==Be&&a!==void 0&&i.push(a)}return i},Ft=(e,t,n,r)=>{let{name:i,data:s,whenCondition:a}=e,l=t.partials||{},c=t.functions||t;if(a&&!F(a,c,n,r))return;if(!l[i])throw new x(`Partial '${i}' is not defined`);let o=r._partialStack||[];if(o.includes(i))throw new x(`Circular partial reference detected: ${i}`);let u=l[i],f=n,p={...r,_partialStack:[...o,i]};if(r){f={...n};for(let h of Object.keys(r))h.startsWith("_")||(f[h]=r[h])}if(s){let h=b(s,t,n,r);f={...f,...h}}return b(u,t,f,p)},Wt=(e,t,n,r)=>{let{path:i}=e,s=i.split("."),a=s[0],l=s.slice(1);if(!r||!(a in r))throw new x(`Path reference '#{${i}}' refers to '${a}' which is not a loop variable in the current scope`);if(r.__paths__||(r.__paths__={}),!(a in r.__paths__))throw new x(`Path reference '#{${i}}' cannot be resolved - path tracking may not be initialized properly`);let c=r.__paths__[a];if(typeof c=="number"){if(l.length>0)throw new x(`Path reference '#{${i}}' - cannot access properties on index variable '${a}'`);return String(c)}return l.length>0&&(c+="."+l.join(".")),c},he=Lt;var zt=/\$\{([^}]*)\}/g,Ut=/#\{([^}]*)\}/g,We=(e,t={})=>{let n=e.match(/^(\w+)\((.*)\)$/);if(!n)return{isFunction:!1};let[,r,i]=n,s=jt(i,t);return{isFunction:!0,type:d.FUNCTION,name:r,args:s}},jt=(e,t={})=>e.trim()?Kt(e).map(r=>Yt(r.trim(),t)):[],Kt=e=>{let t=[],n="",r=0,i=!1,s="";for(let a=0;a<e.length;a++){let l=e[a],c=a>0?e[a-1]:"";!i&&(l==='"'||l==="'")?(i=!0,s=l,n+=l):i&&l===s&&c!=="\\"?(i=!1,s="",n+=l):!i&&l==="("?(r++,n+=l):!i&&l===")"?(r--,n+=l):!i&&l===","&&r===0?(t.push(n),n=""):n+=l}return n&&t.push(n),t},Yt=(e,t={})=>{if(e.startsWith('"')&&e.endsWith('"')||e.startsWith("'")&&e.endsWith("'"))return{type:d.LITERAL,value:e.slice(1,-1)};if(/^-?\d+(\.\d+)?$/.test(e))return{type:d.LITERAL,value:parseFloat(e)};if(e==="true")return{type:d.LITERAL,value:!0};if(e==="false")return{type:d.LITERAL,value:!1};if(e==="null")return{type:d.LITERAL,value:null};let n=We(e,t);if(n.isFunction)return{type:n.type,name:n.name,args:n.args};let r=e.trim(),i=[{op:" + ",type:"ADD"},{op:" - ",type:"SUBTRACT"}],s=-1,a=null;for(let{op:l,type:c}of i){let o=0;for(;o<r.length;){let u=V(r.substring(o),l);if(u===-1)break;let f=o+u;f>s&&(s=f,a={op:l,type:c}),o=f+l.length}}if(s!==-1)try{return $(r,t)}catch{return{type:d.VARIABLE,path:r}}return{type:d.VARIABLE,path:r}},ze=/^\w+\(.*\)$/,Jt=/\s[+\-*/%]\s|\|\||&&|\?\?|.*\?.*:/,Ht=e=>{if(!(!e||e.trim()===""||ze.test(e))&&Jt.test(e))throw e.includes("?")&&e.includes(":")?new _(`Complex expressions not supported in variable replacements - consider calculating the value in your data instead. Offending expression: "${e}"`):e.includes("||")||e.includes("&&")||e.includes("??")?new _(`Logical operators not supported in variable replacements - consider calculating the value in your data instead (operators like ||, &&, ?? are not supported). Offending expression: "${e}"`):new _(`Arithmetic expressions not supported in variable replacements - consider calculating '${e}' in your data instead (expressions with +, -, *, /, % are not supported). Offending expression: "${e}"`)},Fe=e=>{let t=e.trim();if(ze.test(t))throw new _(`Functions are not supported in path references - path references can only refer to loop variables. Offending expression: "#{${e}}"`);if(t.includes("["))throw new _(`Array indices not supported in path references - use simple variable names or properties. Offending expression: "#{${e}}"`);if(/[+\-*/%]/.test(t))throw new _(`Arithmetic expressions not supported in path references - path references can only refer to loop variables. Offending expression: "#{${e}}"`);if(/\|\||&&/.test(t))throw new _(`Logical operators not supported in path references - path references can only refer to loop variables. Offending expression: "#{${e}}"`);if(t.includes("?")&&t.includes(":"))throw new _(`Complex expressions not supported in path references - path references can only refer to loop variables. Offending expression: "#{${e}}"`);return{type:d.PATH_REFERENCE,path:t}},j=(e,t={})=>{let n=e.trim();Ht(n);let r=We(n,t);if(r.isFunction)return{type:r.type,name:r.name,args:r.args};if(n.includes("[")&&!/[\s+\-*/%|&?:]/.test(n)){let i=0;for(let s of n)s==="["?i++:s==="]"&&i--;if(i!==0)throw new Error("Invalid array index syntax")}return{type:d.VARIABLE,path:n}},ye=(e,t={})=>{let n=e,r=[];(e.includes("\\${")||e.includes("\\#{"))&&(n=e.replace(/\\\\(\$\{[^}]*\})/g,"\\DOUBLE_ESC_VAR$1"),n=n.replace(/\\\\(#\{[^}]*\})/g,"\\DOUBLE_ESC_PATH$1"),n=n.replace(/\\(\$\{[^}]*\})/g,(o,u)=>{let f=`__ESCAPED_${r.length}__`;return r.push(u),f}),n=n.replace(/\\(#\{[^}]*\})/g,(o,u)=>{let f=`__ESCAPED_${r.length}__`;return r.push(u),f}),n=n.replace(/\\DOUBLE_ESC_VAR/g,"\\"),n=n.replace(/\\DOUBLE_ESC_PATH/g,"\\"));let i=[...n.matchAll(zt)],s=[...n.matchAll(Ut)],a=[...i.map(o=>({match:o,type:"variable"})),...s.map(o=>({match:o,type:"pathref"}))].sort((o,u)=>o.match.index-u.match.index);if(a.length===0){let o=n;for(let u=0;u<r.length;u++)o=o.replace(`__ESCAPED_${u}__`,r[u]);return{type:d.LITERAL,value:o}}if(a.length===1&&a[0].match[0]===n&&r.length===0){let{match:o,type:u}=a[0];try{return u==="variable"?j(o[1],t):Fe(o[1])}catch(f){if(f.message==="Invalid array index syntax")return{type:d.LITERAL,value:n};throw f}}let l=[],c=0;for(let{match:o,type:u}of a){let[f,p]=o,h=o.index;if(h>c){let y=n.substring(c,h);for(let g=0;g<r.length;g++)y=y.replace(`__ESCAPED_${g}__`,r[g]);y&&l.push(y)}try{let y;u==="variable"?y=j(p.trim(),t):y=Fe(p.trim()),l.push(y)}catch(y){if(y.message==="Invalid array index syntax")l.push(f);else throw y}c=h+f.length}if(c<n.length){let o=n.substring(c);for(let u=0;u<r.length;u++)o=o.replace(`__ESCAPED_${u}__`,r[u]);o&&l.push(o)}return{type:d.INTERPOLATION,parts:l}};var P=(e,t)=>typeof e=="string"?ye(e,t):typeof e=="object"&&e!==null?Array.isArray(e)?Xt(e,t):qt(e,t):{type:d.LITERAL,value:e},Gt=e=>{let{$each:t,...n}=e;if(typeof t!="string")throw new _("$each value must be a non-empty string");let r=t.trim();if(r==="")throw new _("$each value must be a non-empty string");if(Object.keys(n).length===0)throw new _("Empty $each body not allowed");if(n.$partial!==void 0)throw new _("Cannot use $partial with $each at the same level. Wrap $partial in a parent object if you need conditionals.");return{[`$for ${r}`]:n}},Xt=(e,t)=>{let n=[],r=!1;for(let i=0;i<e.length;i++){let s=e[i];if(typeof s=="object"&&s!==null&&!Array.isArray(s)){if(s.$each!==void 0)try{let c=Gt(s),o=Object.keys(c);if(o.length===1&&/^\$for(?::\w+)?\s/.test(o[0])){let u=Ue(o[0],c[o[0]],t,!0);n.push(u),r=!0;continue}}catch(c){throw c instanceof _?c:new _(c.message)}let l=Object.keys(s);if(l.length===1&&/^\$for(?::\w+)?\s/.test(l[0])){let c=Ue(l[0],s[l[0]],t);n.push(c),r=!0;continue}}let a=P(s,t);n.push(a),(a.type===d.FUNCTION||a.type===d.CONDITIONAL||a.type===d.LOOP||a.type===d.PARTIAL||a.type===d.OBJECT&&!a.fast||a.type===d.ARRAY&&!a.fast)&&(r=!0)}return{type:d.ARRAY,items:n,fast:!r}},qt=(e,t)=>{let n=[],r=!1,i=null,s=Object.entries(e),a=0;if(e.$partial!==void 0){if(typeof e.$partial!="string")throw new _("$partial value must be a string");if(e.$partial.trim()==="")throw new _("$partial value cannot be an empty string");let c=["$if","$elif","$else","$for"],o=[];for(let[I]of s)for(let A of c)if(I===A||I.startsWith(A+" ")||I.startsWith(A+"#")){o.push(A);break}if(o.length>0)throw new _(`Cannot use $partial with ${o.join(", ")} at the same level. Wrap $partial in a parent object if you need conditionals.`);let{$partial:u,$when:f,...p}=e,h={},y=!1;for(let[I,A]of Object.entries(p)){let v=I;(I.startsWith("\\$")||I.startsWith("$$"))&&(v=I.slice(1)),h[v]=A,y=!0}let g=null;if(y&&(g=P(h,t),g.type===d.OBJECT)){let I=!1;for(let A of g.properties)if(A.value.type===d.VARIABLE||A.value.type===d.INTERPOLATION||A.value.type===d.FUNCTION||A.value.type===d.CONDITIONAL||A.value.type===d.OBJECT&&!A.value.fast||A.value.type===d.ARRAY&&!A.value.fast){I=!0;break}I&&(g.fast=!1)}let m={type:d.PARTIAL,name:u,data:g};if(f!==void 0){let I;if(typeof f=="string"){if(f.trim()==="")throw new _("Empty condition expression after '$when'");I=$(f,t)}else I={type:d.LITERAL,value:f};m.whenCondition=I}return m}for(let[c,o]of s)if(c==="$when"){if(i!==null)throw new _("Multiple '$when' directives on the same object are not allowed");if(o==null)throw new _("Missing condition expression after '$when'");let u=typeof o=="string"?o:JSON.stringify(o);if(u.trim()==="")throw new _("Empty condition expression after '$when'");i=$(u,t),r=!0}else if(c.startsWith("$when#")||c.startsWith("$when "))throw new _("'$when' does not support ID syntax or inline conditions - use '$when' as a property");for(;a<s.length;){let[c,o]=s[a];if(c==="$when"){a++;continue}if(c==="$each")throw new _("$each can only be used inside arrays");if(c.startsWith("$if ")||c.match(/^\$if#\w+\s/)||c.match(/^\$if\s+\w+.*:$/)){let u=Qt(s,a,t);n.push({key:c,value:u.node}),r=!0,a=u.nextIndex}else if(/^\$for(?::\w+)?\s/.test(c)){let u=c.match(/^\$for(?::(\w+))?\s/)?.[1]||"",f=u?`:${u}`:"";throw new _(`$for loops must be inside arrays - use '- $for${f} item in items:' instead of '$for${f} item in items:'. For cleaner object generation syntax, consider using $each.`)}else{if(c.startsWith("$elif ")||c.startsWith("$else"))throw new _(`'${c.split(" ")[0]}' without matching '$if'`);if(c==="$if"||c==="$if:")throw new _("Missing condition expression after '$if'");{let u=P(o,t);(u.type===d.FUNCTION||u.type===d.CONDITIONAL||u.type===d.PARTIAL||u.type===d.OBJECT&&!u.fast||u.type===d.ARRAY&&!u.fast)&&(r=!0);let f=ye(c,t),p={key:c,value:u};(f.type!==d.LITERAL||f.value!==c)&&(p.parsedKey=f),n.push(p),a++}}}let l={type:d.OBJECT,properties:n,fast:!r};return i&&(l.whenCondition=i),l},Qt=(e,t,n={})=>{let r=[],i=[],s=t,[a,l]=e[s],c=null,o;if(a.startsWith("$if#")){let f=a.match(/^\$if#(\w+)\s+(.+)$/);if(f)c=f[1],o=f[2];else throw new _(`Invalid conditional syntax: ${a}`)}else o=a.substring(4),o.endsWith(":")&&(o=o.slice(0,-1).trim());pe(o);let u=$(o,n);for(r.push(u),i.push(P(l,n)),s++;s<e.length;){let[f,p]=e[s],h=!1,y;if(c?f.startsWith(`$elif#${c} `)?(y=f.substring(`$elif#${c} `.length),y.endsWith(":")&&(y=y.slice(0,-1).trim()),h=!0):(f===`$else#${c}`||f===`$else#${c}:`)&&(h=!0,y=null):f.startsWith("$elif ")?(y=f.substring(6),y.endsWith(":")&&(y=y.slice(0,-1).trim()),h=!0):(f==="$else"||f==="$else:")&&(h=!0,y=null),h){if(y===null)r.push(null);else{pe(y);let g=$(y,n);r.push(g)}if(i.push(P(p,n)),s++,y===null)break}else break}return{node:{type:d.CONDITIONAL,conditions:r,bodies:i,id:c},nextIndex:s}},$=(e,t={})=>{if(e=e.trim(),e.startsWith("(")&&e.endsWith(")")){let o=e.slice(1,-1),u=0,f=!0;for(let p=0;p<o.length;p++)if(o[p]==="("?u++:o[p]===")"&&u--,u<0){f=!1;break}if(f&&u===0)return $(o,t)}let n=V(e,"||");if(n!==-1)return{type:d.BINARY,op:w.OR,left:$(e.substring(0,n).trim(),t),right:$(e.substring(n+2).trim(),t)};let r=V(e,"&&");if(r!==-1)return{type:d.BINARY,op:w.AND,left:$(e.substring(0,r).trim(),t),right:$(e.substring(r+2).trim(),t)};let i=[{op:">=",type:w.GTE},{op:"<=",type:w.LTE},{op:"==",type:w.EQ},{op:"!=",type:w.NEQ},{op:">",type:w.GT},{op:"<",type:w.LT},{op:" in ",type:w.IN}];for(let{op:o,type:u}of i){let f=V(e,o);if(f!==-1)return{type:d.BINARY,op:u,left:$(e.substring(0,f).trim(),t),right:$(e.substring(f+o.length).trim(),t)}}let s=-1,a=null,l=[{op:" + ",type:w.ADD},{op:" - ",type:w.SUBTRACT}];for(let{op:o,type:u}of l){let f=0;for(;f<e.length;){let p=V(e.substring(f),o);if(p===-1)break;let h=f+p;h>s&&(s=h,a={op:o,type:u}),f=h+o.length}}if(s!==-1&&a)return{type:d.BINARY,op:a.type,left:$(e.substring(0,s).trim(),t),right:$(e.substring(s+a.op.length).trim(),t)};let c=[" * "," / "," % "];for(let o of c)if(V(e,o)!==-1)throw new _(`Arithmetic operations are not allowed in conditionals: "${o}"`);return e.startsWith("!")?{type:d.UNARY,op:U.NOT,operand:$(e.substring(1).trim(),t)}:je(e,t)},V=(e,t)=>{let n=0,r=0;for(;r<=e.length-t.length;){if(e[r]==="(")n++;else if(e[r]===")")n--;else if(n===0&&e.substring(r,r+t.length)===t)return r;r++}return-1},Zt=e=>{if(e=e.trim(),e==="true")return{type:d.LITERAL,value:!0};if(e==="false")return{type:d.LITERAL,value:!1};if(e==="null")return{type:d.LITERAL,value:null};if(e.startsWith('"')&&e.endsWith('"')||e.startsWith("'")&&e.endsWith("'"))return{type:d.LITERAL,value:e.slice(1,-1)};if(e==='""'||e==="''")return{type:d.LITERAL,value:""};if(e==="{}")return{type:d.LITERAL,value:{}};if(e==="[]")return{type:d.LITERAL,value:[]};let t=Number(e);return!isNaN(t)&&isFinite(t)?{type:d.LITERAL,value:t}:{type:d.VARIABLE,path:e}},je=(e,t)=>{let n=e.trim();if(n.match(/^(\w+)\((.*)\)$/))return j(n,t);let i=Zt(n);if(i.type===d.LITERAL)return i;if(/^[a-zA-Z_$][\w.$]*$/.test(n))return{type:d.VARIABLE,path:n};try{return j(n,t)}catch(s){if(s.message&&s.message.includes("not supported"))return i;throw s}},en=(e,t=!1)=>{let n=t?"$each":"$for";try{ke(e)}catch(r){if(r instanceof _&&t){let i=r.message.replace(/\$for/g,"$each");throw new _(i.replace("Parse Error: ",""))}throw r}},Ue=(e,t,n,r=!1)=>{let i=/^\$for(?::(\w+))?\s+(.+)$/,s=e.match(i);if(!s){let m=r?"$each":"$for";throw new _(`Invalid loop syntax (got: '${e.replace("$for",m)}')`)}let a=s[1],l=s[2].trim();en(l,r);let c=l.match(/^(.+?)\s+in\s+(.+)$/);if(!c){let m=r?"$each":"$for";throw new _(`Invalid loop syntax - missing 'in' keyword (got: '${m} ${l}')`)}let o=c[1].trim(),u=c[2].trim(),f,p=null;if(o.includes(",")){let m=o.split(",").map(I=>I.trim());if(m.length!==2)throw new _(`Invalid loop variables: ${o}. Expected format: "item" or "item, index"`);f=m[0],p=m[1]}else f=o;let h=["this","undefined","null","true","false"];if(h.includes(f))throw new _(`Reserved variable name: ${f}`);if(p&&h.includes(p))throw new _(`Reserved variable name: ${p}`);let y=je(u,n),g=P(t,n);return{type:d.LOOP,itemVar:f,indexVar:p,iterable:y,body:g,flatten:a!=="nested"}};var tn=(e,t={})=>{let{functions:n={}}=t;return P(e,n)},ee=tn;var ge={};rt(ge,{now:()=>nn});var nn=()=>Date.now();var rn=(e,t,n={})=>{let{functions:r={},partials:i={}}=n,s={...ge,...r},a=ee(e,{functions:s}),l={};for(let[c,o]of Object.entries(i))l[c]=ee(o,{functions:s});return he(a,t,{functions:s,partials:l})},k=rn;var me={objectValues:e=>Object.entries(e).map(([t,n])=>({id:t,...n}))},on=()=>({elements:[{id:"story",type:"container",x:0,y:0,children:[]}],animations:[],audio:[]}),sn=(e,{presentationState:t,resources:n})=>{let{elements:r}=e;if(t.base){let i=r.find(s=>s.id==="story");if(!i)return e;if(t.base.resourceId){let s=n?.layouts[t.base.resourceId];s&&i.children.unshift({id:"base",type:"container",children:s.elements})}}return e},an=(e,{presentationState:t,resources:n={},variables:r,autoMode:i,skipMode:s,currentLocalizationPackageId:a})=>{let{elements:l}=e,c=e.animations||[];if(t.background){let o=l.find(u=>u.id==="story");if(!o)return e;if(t.background.resourceId){let{images:u={}}=n,f=u[t.background.resourceId];f&&o.children.push({id:`bg-cg-${t.background.resourceId}`,type:"sprite",x:0,y:0,src:f.fileId,width:f.width,height:f.height})}if(t.background.resourceId){let{layouts:u={}}=n,f=u[t.background.resourceId];f&&o.children.push({id:`bg-cg-${t.background.resourceId}`,type:"container",children:f.elements})}if(t.background.animations){if(t.background.animations.in){let u=t.background.animations.in.animationId,f=n?.animations[u];f&&c.push({id:"bg-cg-animation-in",type:"tween",targetId:`bg-cg-${t.background.resourceId}`,properties:f.properties})}if(t.background.animations.out){let u=t.background.animations.out.animationId,f=t.background.animations.out.resourceId,p=n?.animations[u];p&&c.push({id:"bg-cg-animation-out",type:"tween",targetId:`bg-cg-${f}`,properties:p.properties})}if(t.background.animations.update){let u=t.background.animations.update.animationId,f=n?.animations[u];f&&c.push({id:"bg-cg-animation-update",type:"tween",targetId:`bg-cg-${t.background.resourceId}`,properties:f.properties})}}}return e},cn=(e,{presentationState:t,resources:n})=>{let{elements:r}=e,i=e.animations||[];if(t.character&&n){let s=r.find(l=>l.id==="story");if(!s)return e;let a=t.character.items||[];for(let l of a){let{transformId:c,sprites:o}=l;if(l.animations&&l.animations.out&&!o&&!c){let h=l.animations.out.animationId,y=n?.animations[h];if(y){let g={id:"character-animation-out",type:"tween",targetId:`character-container-${l.id}`,properties:y.properties};i.push(g)}continue}if(!o||o.length===0||!c){console.warn("Character item missing sprites or transformId:",l);continue}let u=o.map(({resourceId:h})=>h),f=n.transforms[c];if(!f){console.warn("Transform not found:",c);continue}let p={type:"container",id:`character-container-${l.id}`,x:f.x,y:f.y,anchorX:f.anchorX,anchorY:f.anchorY,rotation:f.rotation,scaleX:f.scaleX,scaleY:f.scaleY,children:[]};for(let h of o){let y=n.images[h.resourceId];if(!y){console.warn(`Image resource not found: ${h.resourceId}`);continue}p.children.push({type:"sprite",id:`${l.id}-${h.id}`,src:y.fileId,width:y.width,height:y.height,x:0,y:0})}if(s.children.push(p),l.animations){if(l.animations.in){let h=l.animations.in.animationId,y=n?.animations[h];y&&i.push({id:"character-animation-in",type:"tween",targetId:`character-container-${l.id}`,properties:y.properties})}if(l.animations.update){let h=l.animations.update.animationId,y=n?.animations[h];if(y){let g={id:"character-animation-update",type:"tween",targetId:`character-container-${l.id}`,properties:y.properties};i.push(g)}}}}}return e},ln=(e,{presentationState:t,resources:n})=>{let{elements:r}=e,i=e.animations||[];if(t.visual&&n){let s=r.find(l=>l.id==="story");if(!s)return e;let a=t.visual.items;for(let l of a){if(l.resourceId){let c=n.images[l.resourceId];if(c){let o=n.transforms[l.transformId];s.children.push({id:`visual-${l.id}`,type:"sprite",src:c.fileId,width:c.width,height:c.height,x:o.x,y:o.y,anchorX:o.anchorX,anchorY:o.anchorY,rotation:o.rotation,scaleX:o.scaleX,scaleY:o.scaleY})}}if(l.animations){if(l.animations.in){let c=l.animations.in.animationId||l.animations.in,o=n?.animations[c];o&&i.push({id:`${l.id}-animation`,type:"tween",targetId:`visual-${l.id}`,properties:o.properties})}if(l.animations.out){let c=l.animations.out.animationId||l.animations.out,o=n?.animations[c];o&&i.push({id:`${l.id}-animation-2`,type:"tween",targetId:`visual-${l.id}`,properties:o.properties})}}}}return e},un=(e,{presentationState:t,resources:n={},dialogueUIHidden:r,autoMode:i,skipMode:s,l10n:a,variables:l})=>{let{elements:c}=e;if(!t.dialogue||r)return e;let o=c.find(u=>u.id==="story");if(!o)return e;if(t.dialogue.gui&&t.dialogue.gui.resourceId){let{layouts:u={}}=n,f=u[t.dialogue.gui.resourceId];if(f){let p;t.dialogue.characterId&&(p=n.characters[t.dialogue.characterId]),t.dialogue.character&&(p={...p,name:t.dialogue.character.name});let h={elements:f.elements},y={variables:l,autoMode:i,skipMode:s,dialogue:{character:{name:p?.name||""},content:t.dialogue?.content||[{text:""}],lines:t.dialogue?.lines||[]},l10n:a},g=k(h,y,{functions:me});g=k(g,{l10n:a});let m=g?.elements;if(Array.isArray(m))for(let I of m)o.children.push(structuredClone(I));else m&&o.children.push(structuredClone(m))}}return e},fn=(e,{presentationState:t,resources:n})=>{let{elements:r}=e;if(t.choice&&n){let i=r.find(o=>o.id==="story");if(!i)return e;let s=n?.layouts[t.choice.resourceId];if(!s||!s.elements)return e;let a={elements:s.elements},c=k(a,{choice:{items:t.choice.items}})?.elements;if(Array.isArray(c))for(let o of c)i.children.push(structuredClone(o));else c&&i.children.push(structuredClone(c))}return e},dn=(e,{presentationState:t,resources:n})=>{let{elements:r,audio:i}=e;if(t.bgm&&n){if(!r.find(l=>l.id==="story"))return e;let a=n.audio[t.bgm.audioId];if(!a)return e;i.push({id:"bgm",type:"sound",src:a.fileId,loop:t.bgm.loop??!0,volume:t.bgm.volume??.5,delay:t.bgm.delay??null})}return e},pn=(e,{presentationState:t,resources:n})=>{let{audio:r}=e;if(t.sfx&&n){let i=t.sfx.items;for(let s of i){let a=n.audio?.[s.audioId];a&&r.push({id:s.id,type:"sound",src:a.fileId,loop:s.loop??a.loop??!0,volume:s.volume??a.volume??.5,delay:s.delay??a.delay??null})}}return e},hn=(e,{presentationState:t,resources:n})=>{let{audio:r}=e;if(!t?.voice)return e;let{fileId:i,volume:s,loop:a}=t.voice;return r.push({id:`voice-${i}`,type:"sound",src:i,volume:s??.5,loop:a??!1}),e},yn=(e,{presentationState:t,resources:n={},variables:r,autoMode:i,skipMode:s,currentLocalizationPackageId:a})=>{let{elements:l}=e,c=e.animations||[];if(t.layout){let o=l.find(g=>g.id==="story");if(!o)return e;let u=n.layouts[t.layout.resourceId];if(!u)return e;Array.isArray(u.transitions)&&u.transitions.forEach(g=>{c.push(g)});let f={id:`layout-${t.layout.resourceId}`,type:"container",x:0,y:0,children:u.elements||[]},h=k(f,{variables:r,autoMode:i,skipMode:s,currentLocalizationPackageId:a},{functions:me});h=k(h,{i18n:{}});let y=g=>{let m={...g};return g.src&&g.src.startsWith("file:")&&(m.src=g.src),g.children&&Array.isArray(g.children)&&(m.children=g.children.map(y)),m};o.children.push(y(h))}return e},gn=(e,{resources:t={},variables:n,autoMode:r,skipMode:i,currentLocalizationPackageId:s})=>{let{elements:a}=e,l=e.animations||[],c=[];return c&&c.length>0&&c.forEach((o,u)=>{if(o.resourceType==="layout"){let f=t.layouts[o.resourceId];if(!f){console.warn(`Modal layout not found: ${o.resourceId}`);return}Array.isArray(f.transitions)&&f.transitions.forEach(m=>{l.push(m)});let p={id:`modal-${u}`,type:"container",x:0,y:0,children:f.elements||[]},y=k(p,{variables:n,autoMode:r,skipMode:i,currentLocalizationPackageId:s},{functions:me});y=k(y,{i18n:{}});let g=m=>{let I={...m};if(m.src&&m.src.startsWith("file:")){let A=m.src.replace("file:","");I.src=resolveFile(A)}return m.children&&Array.isArray(m.children)&&(I.children=m.children.map(g)),I};a.push(g(y))}}),e},Ke=e=>Z(on,[sn,an,cn,ln,un,fn,yn,dn,pn,hn,gn])(e);var mn=e=>{let{global:{currentLocalizationPackageId:t},projectData:n}=e,r={sceneId:n.story.initialSceneId,sectionId:n.story.scenes[n.story.initialSceneId].initialSectionId,lineId:n.story.scenes[n.story.initialSceneId].sections[n.story.scenes[n.story.initialSceneId].initialSectionId].lines[0].id};return{projectData:n,global:{isLineCompleted:!1,pendingEffects:[],autoMode:!1,skipMode:!1,dialogueUIHidden:!1,currentLocalizationPackageId:t,viewedRegistry:{sections:[],resources:[]},nextLineConfig:{manual:{enabled:!0,requireLineCompleted:!1},auto:{enabled:!1}},saveSlots:{}},contexts:[{currentPointerMode:"read",pointers:{read:r,history:{sectionId:void 0,lineId:void 0,historySequenceIndex:void 0}},historySequence:[{sectionId:"..."}],configuration:{},views:[],bgm:{resourceId:void 0},variables:{}}]}},_n=({state:e})=>e.global.pendingEffects,Ye=({state:e})=>e.global.skipMode,In=({state:e})=>e.global.autoMode,Je=({state:e})=>e.global.dialogueUIHidden,bn=({state:e})=>e.global.currentLocalizationPackageId,An=({state:e},t)=>{let{sectionId:n,lineId:r}=t,i=e.global.viewedRegistry.sections.find(c=>c.sectionId===n);if(!i)return!1;if(i.lastLineId===void 0||r===void 0)return!0;let s=T({state:e},{sectionId:n});if(!s||!s.lines||!Array.isArray(s.lines))return!1;let a=s.lines.findIndex(c=>c.id===i.lastLineId),l=s.lines.findIndex(c=>c.id===r);return a===-1||l===-1?i.lastLineId===r:l<a},wn=({state:e},t)=>{let{resourceId:n}=t;return!!e.global.viewedRegistry.resources.find(i=>i.resourceId===n)},En=({state:e})=>e.global.nextLineConfig,vn=({state:e})=>e.global.saveSlots,$n=({state:e},t)=>{let{slotKey:n}=t;return e.global.saveSlots[n]},K=({state:e})=>{let t=e.contexts[e.contexts.length-1];if(!t)return;let n=t.pointers?.[t.currentPointerMode];return{currentPointerMode:t.currentPointerMode,pointer:n}},T=({state:e},t)=>{let{sectionId:n}=t,r=e.projectData?.story?.scenes||{};for(let i in r){let s=r[i];if(s.sections&&s.sections[n])return s.sections[n]}},xn=({state:e})=>{let t=K({state:e});if(!t?.pointer)return;let{sectionId:n,lineId:r}=t.pointer,i=T({state:e},{sectionId:n});if(!(!i?.lines||!Array.isArray(i.lines)))return i.lines.find(s=>s.id===r)},He=({state:e})=>{let{sectionId:t,lineId:n}=K({state:e}).pointer,i=T({state:e},{sectionId:t})?.lines||[],s=i.findIndex(o=>o.id===n),a=i.slice(0,s+1);console.log("currentLines",a);let l=a.map(o=>{let u=o.actions||{},f={};return Object.keys(u).forEach(p=>{f[p]=u[p]}),f});return Te(l)},Ln=({state:e})=>{let t=He({state:e});console.log("presentationState",t);let n=Ke({presentationState:t,resources:e.projectData.resources,l10n:e.projectData.l10n.packages[e.global.currentLocalizationPackageId]});return console.log("renderState",n),n},Ge=({state:e})=>(e.global.skipMode&&(e.global.skipMode=!1,e.global.pendingEffects.push({name:"clearSkipNextTimer"})),e.global.autoMode=!0,e.global.pendingEffects.push({name:"clearAutoNextTimer"}),e.global.pendingEffects.push({name:"startAutoNextTimer"}),e.global.pendingEffects.push({name:"render"}),e),Xe=({state:e})=>(e.global.autoMode=!1,e.global.pendingEffects.push({name:"clearAutoNextTimer"}),e.global.pendingEffects.push({name:"render"}),e),Rn=({state:e})=>(e.global.autoMode?Xe({state:e}):Ge({state:e}),e),qe=({state:e})=>(e.global.autoMode&&(e.global.autoMode=!1,e.global.pendingEffects.push({name:"clearAutoNextTimer"})),e.global.skipMode=!0,e.global.pendingEffects.push({name:"clearSkipNextTimer"}),e.global.pendingEffects.push({name:"startSkipNextTimer"}),e.global.pendingEffects.push({name:"render"}),e),Qe=({state:e})=>(e.global.skipMode=!1,e.global.pendingEffects.push({name:"clearSkipNextTimer"}),e.global.pendingEffects.push({name:"render"}),e),On=({state:e})=>(Ye({state:e})?Qe({state:e}):qe({state:e}),e),Ze=({state:e})=>(e.global.dialogueUIHidden=!1,e.global.pendingEffects.push({name:"render"}),e),et=({state:e})=>(e.global.dialogueUIHidden=!0,e.global.pendingEffects.push({name:"render"}),e),Cn=({state:e})=>(Je({state:e})?Ze({state:e}):et({state:e}),e),Tn=({state:e},t)=>{let{localizationPackageId:n}=t;return e.global.currentLocalizationPackageId=n,e.global.pendingEffects.push({name:"render"}),e},kn=({state:e})=>(e.global.pendingEffects=[],e),Pn=({state:e},t)=>(e.global.pendingEffects.push(t),e),Nn=({state:e},t)=>{let{sectionId:n,lineId:r}=t,i=e.global.viewedRegistry.sections.find(s=>s.sectionId===n);return i?i.lastLineId=r:e.global.viewedRegistry.sections.push({sectionId:n,lastLineId:r}),e.global.pendingEffects.push({name:"render"}),e},Sn=({state:e},t)=>{let{resourceId:n}=t;return e.global.viewedRegistry.resources.find(i=>i.resourceId===n)||e.global.viewedRegistry.resources.push({resourceId:n}),e.global.pendingEffects.push({name:"render"}),e},Dn=({state:e},t)=>{let{manual:n,auto:r}=t;return n&&r?e.global.nextLineConfig={manual:n,auto:r}:(n&&(e.global.nextLineConfig.manual=n),r&&(e.global.nextLineConfig.auto=r)),e.global.pendingEffects.push({name:"render"}),e},Vn=({state:e},t)=>{let{slotKey:n,date:r,image:i,state:s}=t;return e.global.saveSlots[n]={slotKey:n,date:r,image:i,state:s},e.global.pendingEffects.push({name:"render"}),e},Mn=({state:e},t)=>{let{projectData:n}=t;return e.projectData=n,e.global.pendingEffects.push({name:"render"}),e},Bn=({state:e},t)=>{let{sectionId:n,lineId:r}=t;if(!r)return console.warn("jumpToLine requires lineId parameter"),e;let i=e.contexts[e.contexts.length-1];if(!i)return console.warn("No context available for jumpToLine"),e;let s=n||i.pointers.read?.sectionId;if(n&&!T({state:e},{sectionId:n}))return console.warn(`Section not found: ${n}`),e;let a=T({state:e},{sectionId:s});return!a?.lines||!Array.isArray(a.lines)?(console.warn(`Section ${s} has no lines`),e):a.lines.find(c=>c.id===r)?(i.pointers.read={sectionId:s,lineId:r},e.global.isLineCompleted=!1,e.global.pendingEffects.push({name:"render"}),e.global.pendingEffects.push({name:"handleLineActions"}),e):(console.warn(`Line not found: ${r} in section ${s}`),e)},Fn=({state:e},t)=>{let{item:n}=t,r=e.contexts[e.contexts.length-1];return r&&r.historySequence&&r.historySequence.push(n),e.global.pendingEffects.push({name:"render"}),e},Wn=({state:e})=>{if(e.global.nextLineConfig?.auto?.enabled!==!0||e.global.nextLineConfig?.auto?.trigger!=="fromComplete")return e;let t=K({state:e})?.pointer,n=t?.sectionId,i=T({state:e},{sectionId:n})?.lines||[],a=i.findIndex(l=>l.id===t?.lineId)+1;if(a<i.length){let l=i[a],c=e.contexts[e.contexts.length-1];c&&(c.pointers.read={sectionId:n,lineId:l.id}),e.global.isLineCompleted=!1,e.global.nextLineConfig.auto={},e.global.pendingEffects.push({name:"render"}),e.global.pendingEffects.push({name:"handleLineActions"})}return e},zn=({state:e})=>{if(!e.global.nextLineConfig.manual.enabled)return e;let t=K({state:e})?.pointer,n=t?.sectionId,i=T({state:e},{sectionId:n})?.lines||[],a=i.findIndex(l=>l.id===t?.lineId)+1;if(a<i.length){let l=i[a],c=e.contexts[e.contexts.length-1];c&&(c.pointers.read={sectionId:n,lineId:l.id}),e.global.isLineCompleted=!1,e.global.pendingEffects.push({name:"render"}),e.global.pendingEffects.push({name:"handleLineActions"})}return e},Un=({state:e})=>(e.global.isLineCompleted=!0,e.global.pendingEffects.push({name:"render"}),e),jn=({state:e},t)=>{let{sectionId:n}=t,r=T({state:e},{sectionId:n});if(!r||!r.lines||r.lines.length===0)return e;let i=r.lines,s=e.contexts[e.contexts.length-1];if(!s||!s.pointers)return e;let a=s.pointers.history||s.pointers.read;if(s.currentPointerMode!=="history"||!s.pointers.history){if(!a)return e;s.currentPointerMode="history",s.pointers.history={sectionId:n,lineId:a?.lineId};let u=i.findIndex(f=>f.id===a.lineId)-1;if(u>=0&&u<i.length){let f=i[u];s.pointers.history={sectionId:n,lineId:f.id}}return e.global.pendingEffects.push({name:"render"}),e}let c=i.findIndex(o=>o.id===s.pointers.history.lineId)-1;if(c>=0&&c<i.length){let o=i[c];s.pointers.history={sectionId:n,lineId:o.id},e.global.pendingEffects.push({name:"render"})}return e},Kn=({state:e},t)=>{let{sectionId:n}=t,r=T({state:e},{sectionId:n});if(!r)return console.warn(`Section not found: ${n}`),e;let i=r.lines?.[0];if(!i)return console.warn(`Section ${n} has no lines`),e;let s=e.contexts[e.contexts.length-1];return s&&(s.pointers.read={sectionId:n,lineId:i.id}),e.global.isLineCompleted=!1,e.global.pendingEffects.push({name:"render"}),e.global.pendingEffects.push({name:"handleLineActions"}),e},tt=e=>{let t=mn(e);return Ce(t,{selectPendingEffects:_n,selectSkipMode:Ye,selectAutoMode:In,selectDialogueUIHidden:Je,selectCurrentLocalizationPackageId:bn,selectIsLineViewed:An,selectIsResourceViewed:wn,selectNextLineConfig:En,selectSaveSlots:vn,selectSaveSlot:$n,selectCurrentPointer:K,selectSection:T,selectCurrentLine:xn,selectPresentationState:He,selectRenderState:Ln,startAutoMode:Ge,stopAutoMode:Xe,toggleAutoMode:Rn,startSkipMode:qe,stopSkipMode:Qe,toggleSkipMode:On,showDialogueUI:Ze,hideDialogueUI:et,toggleDialogueUI:Cn,setCurrentLocalizationPackageId:Tn,clearPendingEffects:kn,appendPendingEffect:Pn,addViewedLine:Nn,addViewedResource:Sn,setNextLineConfig:Dn,replaceSaveSlot:Vn,updateProjectData:Mn,sectionTransition:Kn,jumpToLine:Bn,addToHistory:Fn,nextLine:zn,nextLineFromCompleted:Wn,markLineCompleted:Un,prevLine:jn},{transformActionFirstArgument:r=>({state:r}),transformSelectorFirstArgument:r=>({state:r})})};function _e(e){let t,{handlePendingEffects:n}=e,r=({initialState:o})=>{t=tt(o),t.appendPendingEffect({name:"render"}),c(),n(t.selectPendingEffects()),t.clearPendingEffects()},i=()=>t.selectPresentationState(),s=()=>t.selectRenderState(),a=(o,u)=>{t[o]&&(t[o](u),n(t.selectPendingEffects()),t.clearPendingEffects())},l=o=>{Object.entries(o).forEach(([u,f])=>{a(u,f)})},c=()=>{let o=t.selectCurrentLine();o&&o.actions&&l(o.actions)};return{init:r,handleAction:a,handleActions:l,selectRenderState:s,selectPresentationState:i,handleLineActions:c}}var Vr=_e;export{Vr as default};
|
|
1
|
+
var st=Object.defineProperty;var at=(e,t)=>{for(var n in t)st(e,n,{get:t[n],enumerable:!0})};var ke=Symbol.for("immer-nothing"),Ee=Symbol.for("immer-draftable"),$=Symbol.for("immer-state");function O(e,...t){throw new Error(`[Immer] minified error nr: ${e}. Full error at: https://bit.ly/3cXEKWf`)}var M=Object.getPrototypeOf;function B(e){return!!e&&!!e[$]}function T(e){return e?Ce(e)||Array.isArray(e)||!!e[Ee]||!!e.constructor?.[Ee]||q(e)||G(e):!1}var ct=Object.prototype.constructor.toString();function Ce(e){if(!e||typeof e!="object")return!1;let t=M(e);if(t===null)return!0;let n=Object.hasOwnProperty.call(t,"constructor")&&t.constructor;return n===Object?!0:typeof n=="function"&&Function.toString.call(n)===ct}function Y(e,t){X(e)===0?Reflect.ownKeys(e).forEach(n=>{t(n,e[n],e)}):e.forEach((n,r)=>t(r,n,e))}function X(e){let t=e[$];return t?t.type_:Array.isArray(e)?1:q(e)?2:G(e)?3:0}function oe(e,t){return X(e)===2?e.has(t):Object.prototype.hasOwnProperty.call(e,t)}function Oe(e,t,n){let r=X(e);r===2?e.set(t,n):r===3?e.add(n):e[t]=n}function lt(e,t){return e===t?e!==0||1/e===1/t:e!==e&&t!==t}function q(e){return e instanceof Map}function G(e){return e instanceof Set}function P(e){return e.copy_||e.base_}function se(e,t){if(q(e))return new Map(e);if(G(e))return new Set(e);if(Array.isArray(e))return Array.prototype.slice.call(e);let n=Ce(e);if(t===!0||t==="class_only"&&!n){let r=Object.getOwnPropertyDescriptors(e);delete r[$];let i=Reflect.ownKeys(r);for(let o=0;o<i.length;o++){let a=i[o],c=r[a];c.writable===!1&&(c.writable=!0,c.configurable=!0),(c.get||c.set)&&(r[a]={configurable:!0,writable:!0,enumerable:c.enumerable,value:e[a]})}return Object.create(M(e),r)}else{let r=M(e);if(r!==null&&n)return{...e};let i=Object.create(r);return Object.assign(i,e)}}function fe(e,t=!1){return Q(e)||B(e)||!T(e)||(X(e)>1&&(e.set=e.add=e.clear=e.delete=ut),Object.freeze(e),t&&Object.entries(e).forEach(([n,r])=>fe(r,!0))),e}function ut(){O(2)}function Q(e){return Object.isFrozen(e)}var ft={};function V(e){let t=ft[e];return t||O(0,e),t}var U;function Re(){return U}function dt(e,t){return{drafts_:[],parent_:e,immer_:t,canAutoFreeze_:!0,unfinalizedDrafts_:0}}function ve(e,t){t&&(V("Patches"),e.patches_=[],e.inversePatches_=[],e.patchListener_=t)}function ae(e){ce(e),e.drafts_.forEach(pt),e.drafts_=null}function ce(e){e===U&&(U=e.parent_)}function xe(e){return U=dt(U,e)}function pt(e){let t=e[$];t.type_===0||t.type_===1?t.revoke_():t.revoked_=!0}function Le(e,t){t.unfinalizedDrafts_=t.drafts_.length;let n=t.drafts_[0];return e!==void 0&&e!==n?(n[$].modified_&&(ae(t),O(4)),T(e)&&(e=H(t,e),t.parent_||J(t,e)),t.patches_&&V("Patches").generateReplacementPatches_(n[$].base_,e,t.patches_,t.inversePatches_)):e=H(t,n,[]),ae(t),t.patches_&&t.patchListener_(t.patches_,t.inversePatches_),e!==ke?e:void 0}function H(e,t,n){if(Q(t))return t;let r=t[$];if(!r)return Y(t,(i,o)=>$e(e,r,t,i,o,n)),t;if(r.scope_!==e)return t;if(!r.modified_)return J(e,r.base_,!0),r.base_;if(!r.finalized_){r.finalized_=!0,r.scope_.unfinalizedDrafts_--;let i=r.copy_,o=i,a=!1;r.type_===3&&(o=new Set(i),i.clear(),a=!0),Y(o,(c,l)=>$e(e,r,i,c,l,n,a)),J(e,i,!1),n&&e.patches_&&V("Patches").generatePatches_(r,n,e.patches_,e.inversePatches_)}return r.copy_}function $e(e,t,n,r,i,o,a){if(B(i)){let c=o&&t&&t.type_!==3&&!oe(t.assigned_,r)?o.concat(r):void 0,l=H(e,i,c);if(Oe(n,r,l),B(l))e.canAutoFreeze_=!1;else return}else a&&n.add(i);if(T(i)&&!Q(i)){if(!e.immer_.autoFreeze_&&e.unfinalizedDrafts_<1)return;H(e,i),(!t||!t.scope_.parent_)&&typeof r!="symbol"&&Object.prototype.propertyIsEnumerable.call(n,r)&&J(e,i)}}function J(e,t,n=!1){!e.parent_&&e.immer_.autoFreeze_&&e.canAutoFreeze_&&fe(t,n)}function ht(e,t){let n=Array.isArray(e),r={type_:n?1:0,scope_:t?t.scope_:Re(),modified_:!1,finalized_:!1,assigned_:{},parent_:t,base_:e,draft_:null,copy_:null,revoke_:null,isManual_:!1},i=r,o=de;n&&(i=[r],o=z);let{revoke:a,proxy:c}=Proxy.revocable(i,o);return r.draft_=c,r.revoke_=a,c}var de={get(e,t){if(t===$)return e;let n=P(e);if(!oe(n,t))return gt(e,n,t);let r=n[t];return e.finalized_||!T(r)?r:r===re(e.base_,t)?(ie(e),e.copy_[t]=ue(r,e)):r},has(e,t){return t in P(e)},ownKeys(e){return Reflect.ownKeys(P(e))},set(e,t,n){let r=Ne(P(e),t);if(r?.set)return r.set.call(e.draft_,n),!0;if(!e.modified_){let i=re(P(e),t),o=i?.[$];if(o&&o.base_===n)return e.copy_[t]=n,e.assigned_[t]=!1,!0;if(lt(n,i)&&(n!==void 0||oe(e.base_,t)))return!0;ie(e),le(e)}return e.copy_[t]===n&&(n!==void 0||t in e.copy_)||Number.isNaN(n)&&Number.isNaN(e.copy_[t])||(e.copy_[t]=n,e.assigned_[t]=!0),!0},deleteProperty(e,t){return re(e.base_,t)!==void 0||t in e.base_?(e.assigned_[t]=!1,ie(e),le(e)):delete e.assigned_[t],e.copy_&&delete e.copy_[t],!0},getOwnPropertyDescriptor(e,t){let n=P(e),r=Reflect.getOwnPropertyDescriptor(n,t);return r&&{writable:!0,configurable:e.type_!==1||t!=="length",enumerable:r.enumerable,value:n[t]}},defineProperty(){O(11)},getPrototypeOf(e){return M(e.base_)},setPrototypeOf(){O(12)}},z={};Y(de,(e,t)=>{z[e]=function(){return arguments[0]=arguments[0][0],t.apply(this,arguments)}});z.deleteProperty=function(e,t){return z.set.call(this,e,t,void 0)};z.set=function(e,t,n){return de.set.call(this,e[0],t,n,e[0])};function re(e,t){let n=e[$];return(n?P(n):e)[t]}function gt(e,t,n){let r=Ne(t,n);return r?"value"in r?r.value:r.get?.call(e.draft_):void 0}function Ne(e,t){if(!(t in e))return;let n=M(e);for(;n;){let r=Object.getOwnPropertyDescriptor(n,t);if(r)return r;n=M(n)}}function le(e){e.modified_||(e.modified_=!0,e.parent_&&le(e.parent_))}function ie(e){e.copy_||(e.copy_=se(e.base_,e.scope_.immer_.useStrictShallowCopy_))}var yt=class{constructor(e){this.autoFreeze_=!0,this.useStrictShallowCopy_=!1,this.produce=(t,n,r)=>{if(typeof t=="function"&&typeof n!="function"){let o=n;n=t;let a=this;return function(l=o,...s){return a.produce(l,u=>n.call(this,u,...s))}}typeof n!="function"&&O(6),r!==void 0&&typeof r!="function"&&O(7);let i;if(T(t)){let o=xe(this),a=ue(t,void 0),c=!0;try{i=n(a),c=!1}finally{c?ae(o):ce(o)}return ve(o,r),Le(i,o)}else if(!t||typeof t!="object"){if(i=n(t),i===void 0&&(i=t),i===ke&&(i=void 0),this.autoFreeze_&&fe(i,!0),r){let o=[],a=[];V("Patches").generateReplacementPatches_(t,i,o,a),r(o,a)}return i}else O(1,t)},this.produceWithPatches=(t,n)=>{if(typeof t=="function")return(a,...c)=>this.produceWithPatches(a,l=>t(l,...c));let r,i;return[this.produce(t,n,(a,c)=>{r=a,i=c}),r,i]},typeof e?.autoFreeze=="boolean"&&this.setAutoFreeze(e.autoFreeze),typeof e?.useStrictShallowCopy=="boolean"&&this.setUseStrictShallowCopy(e.useStrictShallowCopy)}createDraft(e){T(e)||O(8),B(e)&&(e=mt(e));let t=xe(this),n=ue(e,void 0);return n[$].isManual_=!0,ce(t),n}finishDraft(e,t){let n=e&&e[$];(!n||!n.isManual_)&&O(9);let{scope_:r}=n;return ve(r,t),Le(void 0,r)}setAutoFreeze(e){this.autoFreeze_=e}setUseStrictShallowCopy(e){this.useStrictShallowCopy_=e}applyPatches(e,t){let n;for(n=t.length-1;n>=0;n--){let i=t[n];if(i.path.length===0&&i.op==="replace"){e=i.value;break}}n>-1&&(t=t.slice(n+1));let r=V("Patches").applyPatches_;return B(e)?r(e,t):this.produce(e,i=>r(i,t))}};function ue(e,t){let n=q(e)?V("MapSet").proxyMap_(e,t):G(e)?V("MapSet").proxySet_(e,t):ht(e,t);return(t?t.scope_:Re()).drafts_.push(n),n}function mt(e){return B(e)||O(10,e),Se(e)}function Se(e){if(!T(e)||Q(e))return e;let t=e[$],n;if(t){if(!t.modified_)return t.base_;t.finalized_=!0,n=se(e,t.scope_.immer_.useStrictShallowCopy_)}else n=se(e,!0);return Y(n,(r,i)=>{Oe(n,r,Se(i))}),t&&(t.finalized_=!1),n}var k=new yt,pe=k.produce,wr=k.produceWithPatches.bind(k),Ar=k.setAutoFreeze.bind(k),Er=k.setUseStrictShallowCopy.bind(k),vr=k.applyPatches.bind(k),xr=k.createDraft.bind(k),Lr=k.finishDraft.bind(k);var Pe=(e,t,n={})=>{let r=structuredClone(e),i={},o={},{transformSelectorFirstArgument:a=l=>l,transformActionFirstArgument:c=l=>l}=n;for(let[l,s]of Object.entries(t))l!=="createInitialState"&&(l.startsWith("select")?i[l]=(...u)=>s(a(r),...u):o[l]=(...u)=>{r=pe(r,d=>s(c(d),...u))});return{...i,...o}},Z=(e,t)=>n=>{let r=e(),i=Array.isArray(n)?n:[n];return pe(r,o=>{i.forEach(a=>{t.forEach(c=>{c(o,a)})})})};var bt=()=>({}),It=(e,t)=>{t.base&&(e.base={...t.base})},_t=(e,t)=>{t.background&&(e.background={...t.background})},wt=(e,t)=>{if(!t.dialogue){e.dialogue&&e.dialogue.mode==="adv"&&(e.dialogue.content=void 0,e.dialogue.characterId=void 0);return}if(e.dialogue||(e.dialogue={}),t.dialogue.gui&&(e.dialogue.gui={...t.dialogue.gui}),t.dialogue.mode==="adv"&&(e.dialogue.content=e.dialogue.content||void 0,e.dialogue.characterId=e.dialogue.characterId||void 0,e.dialogue.mode="adv"),t.dialogue.mode==="nvl"&&(e.dialogue?.mode!=="nvl"&&(e.dialogue.lines=[]),e.dialogue.mode="nvl"),t.dialogue.content!==void 0&&(e.dialogue.content=t.dialogue.content),t.dialogue.characterId!==void 0&&(e.dialogue.characterId=t.dialogue.characterId),t.dialogue.character&&(e.dialogue.character={...t.dialogue.character}),t.dialogue.clear){delete e.dialogue;return}t.dialogue?.mode==="nvl"&&t.dialogue.content!==void 0&&(t.dialogue.clear&&(e.dialogue.lines=[]),e.dialogue.lines.push({content:t.dialogue.content}))},At=(e,t)=>{t.sfx?e.sfx=t.sfx:e.sfx&&delete e.sfx},Et=(e,t)=>{if(t.bgm){if(!t.bgm.resourceId){e.bgm=void 0;return}e.bgm={...t.bgm,loop:t.bgm.loop||t.bgm.loop===void 0}}},vt=(e,t)=>{t.visual&&(e.visual=t.visual)},xt=(e,t)=>{t.character&&(t.character.items||delete e.character,e.character=structuredClone(t.character))},Lt=(e,t)=>{t.animation?e.animation=t.animation:e.animation&&delete e.animation},$t=(e,t)=>{t.layout?e.layout=t.layout:e.layout&&delete e.layout},kt=(e,t)=>{t.choice?e.choice=t.choice:e.choice&&delete e.choice},Ct=(e,t)=>{t.keyboard&&(e.keyboard=t.keyboard)},Ot=(e,t)=>{t.voice?e.voice=t.voice:e.voice&&delete e.voice},Rt=(e,t)=>{t.cleanAll&&Object.keys(e).forEach(n=>{delete e[n]})},he=e=>Z(bt,[Rt,It,_t,At,Et,vt,wt,xt,Lt,$t,kt,Ct,Ot])(e);var p={LITERAL:0,VARIABLE:1,INTERPOLATION:2,FUNCTION:3,BINARY:4,UNARY:5,CONDITIONAL:6,LOOP:7,OBJECT:8,ARRAY:9,PARTIAL:10,PATH_REFERENCE:11},A={EQ:0,NEQ:1,GT:2,LT:3,GTE:4,LTE:5,AND:6,OR:7,IN:8,ADD:10,SUBTRACT:11},j={NOT:0};var b=class extends Error{constructor(t){super(`Parse Error: ${t}`),this.name="JemplParseError"}},L=class extends Error{constructor(t){super(`Render Error: ${t}`),this.name="JemplRenderError"}};var ge=e=>{if(!e||e.trim()==="")throw new b("Missing condition expression after '$if'");if(e.includes("===")||e.includes("!==")){let n=e.includes("===")?"==":"!=";throw new b(`Invalid comparison operator '${e.includes("===")?"===":"!=="}' - did you mean '${n}'? (got: '${e}')`)}let t=["<",">","<=",">=","==","!="];for(let n of t)if(e.trim().endsWith(n))throw new b(`Incomplete comparison expression - missing right operand (got: '${e}')`)},Te=e=>{if(e.trim().endsWith(" in"))throw new b(`Missing iterable expression after 'in' (got: '$for ${e}')`);if(!e.includes(" in "))throw new b(`Invalid loop syntax - missing 'in' keyword (got: '$for ${e}')`);let[t,n]=e.split(" in ");if(!n||n.trim()==="")throw new b(`Missing iterable expression after 'in' (got: '$for ${e}')`);let r=t.includes(",")?t.split(",").map(i=>i.trim()):[t.trim()];for(let i of r){if(!i)throw new b(`Invalid loop variable - variable name cannot be empty (got: '$for ${e}')`);if(!/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(i))throw new b(`Invalid loop syntax (got: '$for ${e}')`)}};var Ve=(e,t,n=!1)=>{if(t===null)return new L(`Cannot iterate over null value at '$for ${e}'`);if(t===void 0)return new L(`Cannot iterate over undefined value at '$for ${e}'`);let r=typeof t;return n?new L(`Cannot iterate over non-array value in loop '${e}' - got ${r} instead`):new L(`Cannot iterate over non-array value (got: ${r}) at '$for ${e}'`)},De=(e,t)=>{let n=t&&Object.keys(t).length>0?Object.keys(t).join(", "):"no custom functions provided";return new L(`Unknown function '${e}' (${n})`)};var Nt=(e,t,n={})=>{let r={},i={};n&&typeof n=="object"&&(n.functions!==void 0||n.partials!==void 0?(r=n.functions||{},i=n.partials||{}):typeof n=="object"&&(r=n));let a=w(e,{functions:r,partials:i},t,{});return a===void 0?{}:a},w=(e,t,n,r)=>{let i=t.functions||t;if(e.var&&!e.type)return E(e.var,n,r);if(e.type===p.LITERAL)return e.value;if(e.type===p.VARIABLE)return E(e.path,n,r);if(e.type===p.INTERPOLATION)return Pt(e.parts,t,n,r);switch(e.type){case p.FUNCTION:return Be(e,t,n,r);case p.BINARY:return Fe(e,t,n,r);case p.UNARY:return We(e,t,n,r);case p.CONDITIONAL:return Vt(e,t,n,r);case p.LOOP:return Wt(e,t,n,r);case p.OBJECT:return zt(e,t,n,r);case p.ARRAY:return jt(e,t,n,r);case p.PARTIAL:return Kt(e,t,n,r);case p.PATH_REFERENCE:return Yt(e,t,n,r);default:throw new Error(`Unknown node type: ${e.type}`)}},Me=new Map,St=e=>{let t=[],n="",r=!1;for(let i=0;i<e.length;i++){let o=e[i];if(o==="[")n&&(t.push({type:"property",value:n}),n=""),r=!0;else if(o==="]"){if(r&&n){let a=n.trim();if(/^\d+$/.test(a))t.push({type:"index",value:parseInt(a,10)});else if(a.startsWith('"')&&a.endsWith('"')||a.startsWith("'")&&a.endsWith("'")){let c=a.slice(1,-1);t.push({type:"property",value:c})}else t.push({type:"property",value:a});n=""}r=!1}else n+=o}return n&&t.push({type:"property",value:n}),t},E=(e,t,n)=>{if(!e)return;if(e in n)return n[e];let r=Me.get(e);if(!r){let o=[],a="",c=0;for(let l=0;l<e.length;l++){let s=e[l];s==="["?(c++,a+=s):s==="]"?(c--,a+=s):s==="."&&c===0?a&&(o.push(a),a=""):a+=s}a&&o.push(a),r=[];for(let l of o){let s=St(l.trim());r.push(...s)}Me.set(e,r)}let i=t;for(let o=0;o<r.length;o++){let a=r[o];if(a.type==="property"&&a.value in n){i=n[a.value];continue}if(i==null)return;(a.type==="property"||a.type==="index")&&(i=i[a.value])}return i},Pt=(e,t,n,r)=>{let i=[];for(let o of e)if(typeof o=="string")i.push(o);else{let a=w(o,t,n,r);i.push(a!=null?String(a):"")}return i.join("")},Be=(e,t,n,r)=>{let i=t.functions||t,o=i[e.name];if(!o)throw De(e.name,i);let a=e.args.map(c=>w(c,t,n,r));return o(...a)},F=(e,t,n,r)=>{if(e.var&&!e.type)return E(e.var,n,r);switch(e.type){case p.VARIABLE:return E(e.path,n,r);case p.LITERAL:return e.value;case p.BINARY:return Fe(e,t,n,r);case p.UNARY:return We(e,t,n,r);case p.FUNCTION:return Be(e,t,n,r);default:return w(e,t,n,r)}},Fe=(e,t,n,r)=>{if(e.op===A.AND||e.op===A.OR){let a=F(e.left,t,n,r),c=F(e.right,t,n,r);switch(e.op){case A.AND:return a&&c;case A.OR:return a||c}}let i=w(e.left,t,n,r),o=w(e.right,t,n,r);switch(e.op){case A.EQ:return i==o;case A.NEQ:return i!=o;case A.GT:return i>o;case A.LT:return i<o;case A.GTE:return i>=o;case A.LTE:return i<=o;case A.IN:return Array.isArray(o)?o.includes(i):!1;case A.ADD:if(typeof i!="number"||typeof o!="number")throw new L(`Arithmetic operations require numbers. Got ${typeof i} + ${typeof o}`);return i+o;case A.SUBTRACT:if(typeof i!="number"||typeof o!="number")throw new L(`Arithmetic operations require numbers. Got ${typeof i} - ${typeof o}`);return i-o;default:throw new Error(`Unknown binary operator: ${e.op}`)}},We=(e,t,n,r)=>{let i=e.op===j.NOT?F(e.operand,t,n,r):w(e.operand,t,n,r);if(e.op===j.NOT)return!i;throw new Error(`Unknown unary operator: ${e.op}`)},Tt=(e,t,n,r)=>{if(e.conditions.length===2&&e.conditions[1]===null){let i=e.conditions[0];if(i.type===p.VARIABLE)if(E(i.path,n,r)){let a=e.bodies[0];if(a.type===p.OBJECT&&a.properties.length<=5){let c={};for(let l of a.properties){let s=l.parsedKey?w(l.parsedKey,t,n,r):l.key,u=l.value;if(u.type===p.LITERAL)c[s]=u.value;else if(u.type===p.VARIABLE)c[s]=E(u.path,n,r);else if(u.type===p.INTERPOLATION){let f=[];for(let d of u.parts)if(typeof d=="string")f.push(d);else if(d.type===p.VARIABLE){let h=E(d.path,n,r);f.push(h!=null?String(h):"")}else{let h=w(d,t,n,r);f.push(h!=null?String(h):"")}c[s]=f.join("")}else c[s]=w(u,t,n,r)}return c}}else{let a=e.bodies[1];if(a.type===p.OBJECT&&a.properties.length<=5){let c={};for(let l of a.properties){let s=l.parsedKey?w(l.parsedKey,t,n,r):l.key,u=l.value;if(u.type===p.LITERAL)c[s]=u.value;else if(u.type===p.VARIABLE)c[s]=E(u.path,n,r);else if(u.type===p.INTERPOLATION){let f=[];for(let d of u.parts)if(typeof d=="string")f.push(d);else if(d.type===p.VARIABLE){let h=E(d.path,n,r);f.push(h!=null?String(h):"")}else{let h=w(d,t,n,r);f.push(h!=null?String(h):"")}c[s]=f.join("")}else c[s]=w(u,t,n,r)}return c}}}return null},Vt=(e,t,n,r)=>{let i=Tt(e,t,n,r);if(i!==null)return i;for(let o=0;o<e.conditions.length;o++){let a=e.conditions[o];if(a===null||F(a,t,n,r))return w(e.bodies[o],t,n,r)}return Ue},Dt=(e,t)=>{let n=e.body,r=e.itemVar;if(n.type===p.CONDITIONAL&&n.conditions.length===1&&n.conditions[0].type===p.VARIABLE){let i=n.conditions[0].path,o=n.bodies[0];if(i.startsWith(r+".")){let a=i.substring(r.length+1);if(o.type===p.OBJECT&&o.properties.length<=5){for(let l of o.properties)if(l.parsedKey)return null;let c=[];for(let l=0;l<t.length;l++){let s=t[l];if(s[a]){let u={};for(let f of o.properties){let d=f.key,h=f.value;if(h.type===p.LITERAL)u[d]=h.value;else if(h.type===p.VARIABLE){let g=h.path;if(g===r)u[d]=s;else if(g.startsWith(r+".")){let y=g.substring(r.length+1);u[d]=s[y]}else return null}else if(h.type===p.INTERPOLATION){let g=[],y=!0;for(let I of h.parts)if(typeof I=="string")g.push(I);else if(I.type===p.VARIABLE){let m=I.path;if(m===r)g.push(s!=null?String(s):"");else if(m.startsWith(r+".")){let _=m.substring(r.length+1),v=s[_];g.push(v!=null?String(v):"")}else{y=!1;break}}else{y=!1;break}if(!y)return null;u[d]=g.join("")}else return null}c.push(u)}}return c}}}return null},Mt=(e,t)=>{let n=e.body,r=e.itemVar,i=Dt(e,t);if(i!==null)return i;if(n.type===p.OBJECT&&n.properties.length<=5&&!n.whenCondition){for(let c of n.properties)if(c.parsedKey)return null;let o=[],a=!0;for(let c of n.properties){let l=c.key,s=c.value;if(s.type===p.LITERAL)o.push({key:l,type:"literal",value:s.value});else if(s.type===p.VARIABLE){let u=s.path;if(u===r)o.push({key:l,type:"item"});else if(u.startsWith(r+".")){let f=u.substring(r.length+1);if(!f.includes(".")&&!f.includes("["))o.push({key:l,type:"prop",prop:f});else{a=!1;break}}else{a=!1;break}}else if(s.type===p.INTERPOLATION&&s.parts.length===1){let u=s.parts[0];if(u.type===p.VARIABLE){let f=u.path;if(f===r)o.push({key:l,type:"item_string"});else if(f.startsWith(r+".")){let d=f.substring(r.length+1);if(!d.includes(".")&&!d.includes("["))o.push({key:l,type:"prop_string",prop:d});else{a=!1;break}}else{a=!1;break}}else{a=!1;break}}else{a=!1;break}}if(a){let c=new Array(t.length);if(o.length===3&&o[0].type==="prop"&&o[0].key==="id"&&o[1].type==="prop_string"&&o[1].key==="title"&&o[2].type==="prop"&&o[2].key==="completed")for(let l=0;l<t.length;l++){let s=t[l];c[l]={id:s.id,title:s.title!=null?String(s.title):"",completed:s.completed}}else for(let l=0;l<t.length;l++){let s=t[l],u={};for(let f of o)if(f.type==="literal")u[f.key]=f.value;else if(f.type==="item")u[f.key]=s;else if(f.type==="prop")u[f.key]=s[f.prop];else if(f.type==="item_string")u[f.key]=s!=null?String(s):"";else if(f.type==="prop_string"){let d=s[f.prop];u[f.key]=d!=null?String(d):""}c[l]=u}return c}}return null},Bt=(e,t,n,r,i)=>{let o=[],a=e.body;if(a.type===p.OBJECT&&a.fast!==!1){let c=e.itemVar,l=e.indexVar;for(let s=0;s<i.length;s++){let u=i[s],f={},d={...r,[c]:u,...l&&{[l]:s}};d.__paths__||(d.__paths__=r.__paths__||{});let h=e.iterable.path||"";if(r&&r.__paths__&&h){let g=h.split("."),y=g[0];y in r.__paths__&&(h=r.__paths__[y],g.length>1&&(h+="."+g.slice(1).join(".")))}d.__paths__={...d.__paths__,[c]:`${h}[${s}]`,...l&&{[l]:s}};for(let g of a.properties){let y=g.parsedKey?w(g.parsedKey,t,n,d):g.key,I=g.value;if(I.type===p.LITERAL)f[y]=I.value;else if(I.type===p.VARIABLE){let m=I.path;if(m===c)f[y]=u;else if(m===l)f[y]=s;else if(m.startsWith(c+".")){let _=m.substring(c.length+1);!_.includes(".")&&!_.includes("[")?f[y]=u[_]:f[y]=E(m,n,{...r,[c]:u,...l&&{[l]:s}})}else f[y]=E(m,n,{...r,[c]:u,...l&&{[l]:s}})}else if(I.type===p.INTERPOLATION){let m=[];for(let _ of I.parts)if(typeof _=="string")m.push(_);else if(_.type===p.VARIABLE){let v=_.path,R;if(v===c)R=u;else if(v===l)R=s;else if(v.startsWith(c+".")){let ne=v.substring(c.length+1);!ne.includes(".")&&!ne.includes("[")?R=u[ne]:R=E(v,n,{...r,[c]:u,...l&&{[l]:s}})}else R=E(v,n,{...r,[c]:u,...l&&{[l]:s}});m.push(R!=null?String(R):"")}else{let v={...r,[c]:u,...l&&{[l]:s}},R=w(_,t,n,v);m.push(R!=null?String(R):"")}f[y]=m.join("")}else{let m={...r,[c]:u,...l&&{[l]:s}};f[y]=w(I,t,n,m)}}o.push(f)}return o}return null},Ft=(e,t,n)=>{let r=e.body;if(r.type===p.OBJECT&&r.properties.length===1&&r.properties[0].key==="$if item.visible"){let o=r.properties[0].value;if(o.type===p.CONDITIONAL&&o.conditions.length===1&&o.conditions[0].type===p.VARIABLE&&o.conditions[0].path==="item.visible"){let a=o.bodies[0];if(a.type===p.OBJECT&&a.properties.length===2){let c=a.properties[0],l=a.properties[1];if(c.key==="id"&&c.value.type===p.VARIABLE&&c.value.path==="item.id"&&l.key==="$if item.highlighted"&&l.value.type===p.CONDITIONAL){let s=[];for(let u=0;u<t.length;u++){let f=t[u];if(f.visible){let d={id:f.id};f.highlighted?(d.highlight=!0,d.message=`This item is highlighted: ${f.name}`):(d.highlight=!1,d.message=f.name),s.push(d)}}return s}}}}return null},Wt=(e,t,n,r)=>{let i=w(e.iterable,t,n,r);if(!Array.isArray(i)){let s,u=!1;if(e.iterable.type===p.FUNCTION){u=!0;let d=e.iterable.args.map(h=>h.type===p.LITERAL?typeof h.value=="string"?`'${h.value}'`:String(h.value):h.type===p.VARIABLE?h.path:h.type===p.FUNCTION?`${h.name}(...)`:"?").join(", ");s=`${e.iterable.name}(${d})`}else s=e.iterable.path||"undefined";let f=`${e.itemVar}${e.indexVar?`, ${e.indexVar}`:""} in ${s}`;throw Ve(f,i,u)}if(!e.indexVar){let s=Ft(e,i,e.itemVar);if(s!==null)return s}if(!e.indexVar){let s=Mt(e,i);if(s!==null)return s}let o=Bt(e,t,n,r,i);if(o!==null)return o;let a=[],c=e.iterable.path||"";if(r&&r.__paths__&&c){let s=c.split("."),u=s[0];u in r.__paths__&&(c=r.__paths__[u],s.length>1&&(c+="."+s.slice(1).join(".")))}let l=!1;e.body.type===p.ARRAY&&(e.body.items.length<=1?l=!1:l=e.body._shouldPreserveArray??=e.body.items.some(s=>s.type===p.OBJECT&&s.properties.some(u=>u.key.startsWith("$if ")||u.key.startsWith("$when "))));for(let s=0;s<i.length;s++){let u=e.indexVar?{...r,[e.itemVar]:i[s],[e.indexVar]:s}:{...r,[e.itemVar]:i[s]};u.__paths__||(u.__paths__=r.__paths__||{}),u.__paths__={...u.__paths__,[e.itemVar]:`${c}[${s}]`},e.indexVar&&(u.__paths__[e.indexVar]=s);let f=w(e.body,t,n,u);if(Array.isArray(f)&&f.length===1&&!l){let d=f[0];a.push(d===void 0?{}:d)}else a.push(f===void 0?{}:f)}return a},Ut=(e,t,n,r)=>{if(e.whenCondition)return null;if(e.properties.length===1){let i=e.properties[0],o=i.parsedKey?w(i.parsedKey,t,n,r):i.key,a=i.value;if(a.type===p.OBJECT&&a.properties.length<=10&&!a.whenCondition){let c={},l={},s=!0;for(let u of a.properties){let f=u.parsedKey?w(u.parsedKey,t,n,r):u.key,d=u.value;if(d.type===p.LITERAL)l[f]=d.value;else if(d.type===p.VARIABLE)l[f]=E(d.path,n,r);else if(d.type===p.INTERPOLATION){let h=[];for(let g of d.parts)if(typeof g=="string")h.push(g);else if(g.type===p.VARIABLE){let y=E(g.path,n,r);h.push(y!=null?String(y):"")}else{s=!1;break}if(!s)break;l[f]=h.join("")}else if(d.type===p.OBJECT&&d.properties.length<=5){let h={};for(let g of d.properties){let y=g.key,I=g.value;if(I.type===p.LITERAL)h[y]=I.value;else if(I.type===p.VARIABLE)h[y]=E(I.path,n,r);else if(I.type===p.INTERPOLATION){let m=[];for(let _ of I.parts)if(typeof _=="string")m.push(_);else if(_.type===p.VARIABLE){let v=E(_.path,n,r);m.push(v!=null?String(v):"")}else{s=!1;break}if(!s)break;h[y]=m.join("")}else{s=!1;break}}if(!s)break;l[f]=h}else{s=!1;break}}if(s)return c[o]=l,c}}return null},zt=(e,t,n,r)=>{let i=t.functions||t;if(e.whenCondition&&!F(e.whenCondition,i,n,r))return;let o=Ut(e,t,n,r);if(o!==null)return o;if(e.fast){let s={};for(let u of e.properties){let f=u.parsedKey?w(u.parsedKey,t,n,r):u.key,d=u.value;if(d.type===p.LITERAL)s[f]=d.value;else if(d.type===p.VARIABLE)s[f]=E(d.path,n,r);else if(d.type===p.INTERPOLATION){let h=[];for(let g of d.parts)if(typeof g=="string")h.push(g);else if(g.type===p.VARIABLE){let y=E(g.path,n,r);h.push(y!=null?String(y):"")}else{let y=w(g,t,n,r);h.push(y!=null?String(y):"")}s[f]=h.join("")}else s[f]=w(d,t,n,r)}return s}let a={},c=null,l=!1;for(let s of e.properties)if(!s.key.startsWith("$if ")&&!s.key.match(/^\$if\s+\w+.*:?$/)&&!s.key.startsWith("$elif")&&!s.key.startsWith("$else")&&!s.key.startsWith("$for ")){l=!0;break}for(let s of e.properties)if(s.key.startsWith("$if ")||s.key.match(/^\$if\s+\w+.*:?$/)){let u=w(s.value,t,n,r);if(!l&&u!==null&&u!==void 0)return Array.isArray(u)&&u.length===1?u[0]:u;typeof u=="object"&&u!==null&&!Array.isArray(u)&&Object.assign(a,u)}else if(s.key.startsWith("$for ")){if(e.properties.length===1)return w(s.value,t,n,r)}else{let u=s.value;if(u&&u.type===p.OBJECT&&u.properties){let f=u.properties.find(d=>d.key.startsWith("$for "));if(f){let d=w(f.value,t,n,r);d!==void 0&&(a[s.key]=d)}else{let d=w(s.value,t,n,r);d!==void 0&&(a[s.key]=d)}}else{let f=s.parsedKey?w(s.parsedKey,t,n,r):s.key,d=w(s.value,t,n,r);d!==void 0&&(a[f]=d)}}return a},Ue={},jt=(e,t,n,r)=>{let i=[];for(let o of e.items)if(o.type===p.LOOP){let a=w(o,t,n,r);Array.isArray(a)&&o.flatten!==!1?i.push(...a):i.push(a)}else{let a=w(o,t,n,r);a!==Ue&&a!==void 0&&i.push(a)}return i},Kt=(e,t,n,r)=>{let{name:i,data:o,whenCondition:a}=e,c=t.partials||{},l=t.functions||t;if(a&&!F(a,l,n,r))return;if(!c[i])throw new L(`Partial '${i}' is not defined`);let s=r._partialStack||[];if(s.includes(i))throw new L(`Circular partial reference detected: ${i}`);let u=c[i],f=n,d={...r,_partialStack:[...s,i]};if(r){f={...n};for(let h of Object.keys(r))h.startsWith("_")||(f[h]=r[h])}if(o){let h=w(o,t,n,r);f={...f,...h}}return w(u,t,f,d)},Yt=(e,t,n,r)=>{let{path:i}=e,o=i.split("."),a=o[0],c=o.slice(1);if(!r||!(a in r))throw new L(`Path reference '#{${i}}' refers to '${a}' which is not a loop variable in the current scope`);if(r.__paths__||(r.__paths__={}),!(a in r.__paths__))throw new L(`Path reference '#{${i}}' cannot be resolved - path tracking may not be initialized properly`);let l=r.__paths__[a];if(typeof l=="number"){if(c.length>0)throw new L(`Path reference '#{${i}}' - cannot access properties on index variable '${a}'`);return String(l)}return c.length>0&&(l+="."+c.join(".")),l},ye=Nt;var Ht=/\$\{([^}]*)\}/g,Jt=/#\{([^}]*)\}/g,je=(e,t={})=>{let n=e.match(/^(\w+)\((.*)\)$/);if(!n)return{isFunction:!1};let[,r,i]=n,o=Xt(i,t);return{isFunction:!0,type:p.FUNCTION,name:r,args:o}},Xt=(e,t={})=>e.trim()?qt(e).map(r=>Gt(r.trim(),t)):[],qt=e=>{let t=[],n="",r=0,i=!1,o="";for(let a=0;a<e.length;a++){let c=e[a],l=a>0?e[a-1]:"";!i&&(c==='"'||c==="'")?(i=!0,o=c,n+=c):i&&c===o&&l!=="\\"?(i=!1,o="",n+=c):!i&&c==="("?(r++,n+=c):!i&&c===")"?(r--,n+=c):!i&&c===","&&r===0?(t.push(n),n=""):n+=c}return n&&t.push(n),t},Gt=(e,t={})=>{if(e.startsWith('"')&&e.endsWith('"')||e.startsWith("'")&&e.endsWith("'"))return{type:p.LITERAL,value:e.slice(1,-1)};if(/^-?\d+(\.\d+)?$/.test(e))return{type:p.LITERAL,value:parseFloat(e)};if(e==="true")return{type:p.LITERAL,value:!0};if(e==="false")return{type:p.LITERAL,value:!1};if(e==="null")return{type:p.LITERAL,value:null};let n=je(e,t);if(n.isFunction)return{type:n.type,name:n.name,args:n.args};let r=e.trim(),i=[{op:" + ",type:"ADD"},{op:" - ",type:"SUBTRACT"}],o=-1,a=null;for(let{op:c,type:l}of i){let s=0;for(;s<r.length;){let u=D(r.substring(s),c);if(u===-1)break;let f=s+u;f>o&&(o=f,a={op:c,type:l}),s=f+c.length}}if(o!==-1)try{return x(r,t)}catch{return{type:p.VARIABLE,path:r}}return{type:p.VARIABLE,path:r}},Ke=/^\w+\(.*\)$/,Qt=/\s[+\-*/%]\s|\|\||&&|\?\?|.*\?.*:/,Zt=e=>{if(!(!e||e.trim()===""||Ke.test(e))&&Qt.test(e))throw e.includes("?")&&e.includes(":")?new b(`Complex expressions not supported in variable replacements - consider calculating the value in your data instead. Offending expression: "${e}"`):e.includes("||")||e.includes("&&")||e.includes("??")?new b(`Logical operators not supported in variable replacements - consider calculating the value in your data instead (operators like ||, &&, ?? are not supported). Offending expression: "${e}"`):new b(`Arithmetic expressions not supported in variable replacements - consider calculating '${e}' in your data instead (expressions with +, -, *, /, % are not supported). Offending expression: "${e}"`)},ze=e=>{let t=e.trim();if(Ke.test(t))throw new b(`Functions are not supported in path references - path references can only refer to loop variables. Offending expression: "#{${e}}"`);if(t.includes("["))throw new b(`Array indices not supported in path references - use simple variable names or properties. Offending expression: "#{${e}}"`);if(/[+\-*/%]/.test(t))throw new b(`Arithmetic expressions not supported in path references - path references can only refer to loop variables. Offending expression: "#{${e}}"`);if(/\|\||&&/.test(t))throw new b(`Logical operators not supported in path references - path references can only refer to loop variables. Offending expression: "#{${e}}"`);if(t.includes("?")&&t.includes(":"))throw new b(`Complex expressions not supported in path references - path references can only refer to loop variables. Offending expression: "#{${e}}"`);return{type:p.PATH_REFERENCE,path:t}},K=(e,t={})=>{let n=e.trim();Zt(n);let r=je(n,t);if(r.isFunction)return{type:r.type,name:r.name,args:r.args};if(n.includes("[")&&!/[\s+\-*/%|&?:]/.test(n)){let i=0;for(let o of n)o==="["?i++:o==="]"&&i--;if(i!==0)throw new Error("Invalid array index syntax")}return{type:p.VARIABLE,path:n}},me=(e,t={})=>{let n=e,r=[];(e.includes("\\${")||e.includes("\\#{"))&&(n=e.replace(/\\\\(\$\{[^}]*\})/g,"\\DOUBLE_ESC_VAR$1"),n=n.replace(/\\\\(#\{[^}]*\})/g,"\\DOUBLE_ESC_PATH$1"),n=n.replace(/\\(\$\{[^}]*\})/g,(s,u)=>{let f=`__ESCAPED_${r.length}__`;return r.push(u),f}),n=n.replace(/\\(#\{[^}]*\})/g,(s,u)=>{let f=`__ESCAPED_${r.length}__`;return r.push(u),f}),n=n.replace(/\\DOUBLE_ESC_VAR/g,"\\"),n=n.replace(/\\DOUBLE_ESC_PATH/g,"\\"));let i=[...n.matchAll(Ht)],o=[...n.matchAll(Jt)],a=[...i.map(s=>({match:s,type:"variable"})),...o.map(s=>({match:s,type:"pathref"}))].sort((s,u)=>s.match.index-u.match.index);if(a.length===0){let s=n;for(let u=0;u<r.length;u++)s=s.replace(`__ESCAPED_${u}__`,r[u]);return{type:p.LITERAL,value:s}}if(a.length===1&&a[0].match[0]===n&&r.length===0){let{match:s,type:u}=a[0];try{return u==="variable"?K(s[1],t):ze(s[1])}catch(f){if(f.message==="Invalid array index syntax")return{type:p.LITERAL,value:n};throw f}}let c=[],l=0;for(let{match:s,type:u}of a){let[f,d]=s,h=s.index;if(h>l){let g=n.substring(l,h);for(let y=0;y<r.length;y++)g=g.replace(`__ESCAPED_${y}__`,r[y]);g&&c.push(g)}try{let g;u==="variable"?g=K(d.trim(),t):g=ze(d.trim()),c.push(g)}catch(g){if(g.message==="Invalid array index syntax")c.push(f);else throw g}l=h+f.length}if(l<n.length){let s=n.substring(l);for(let u=0;u<r.length;u++)s=s.replace(`__ESCAPED_${u}__`,r[u]);s&&c.push(s)}return{type:p.INTERPOLATION,parts:c}};var S=(e,t)=>typeof e=="string"?me(e,t):typeof e=="object"&&e!==null?Array.isArray(e)?tn(e,t):nn(e,t):{type:p.LITERAL,value:e},en=e=>{let{$each:t,...n}=e;if(typeof t!="string")throw new b("$each value must be a non-empty string");let r=t.trim();if(r==="")throw new b("$each value must be a non-empty string");if(Object.keys(n).length===0)throw new b("Empty $each body not allowed");if(n.$partial!==void 0)throw new b("Cannot use $partial with $each at the same level. Wrap $partial in a parent object if you need conditionals.");return{[`$for ${r}`]:n}},tn=(e,t)=>{let n=[],r=!1;for(let i=0;i<e.length;i++){let o=e[i];if(typeof o=="object"&&o!==null&&!Array.isArray(o)){if(o.$each!==void 0)try{let l=en(o),s=Object.keys(l);if(s.length===1&&/^\$for(?::\w+)?\s/.test(s[0])){let u=Ye(s[0],l[s[0]],t,!0);n.push(u),r=!0;continue}}catch(l){throw l instanceof b?l:new b(l.message)}let c=Object.keys(o);if(c.length===1&&/^\$for(?::\w+)?\s/.test(c[0])){let l=Ye(c[0],o[c[0]],t);n.push(l),r=!0;continue}}let a=S(o,t);n.push(a),(a.type===p.FUNCTION||a.type===p.CONDITIONAL||a.type===p.LOOP||a.type===p.PARTIAL||a.type===p.OBJECT&&!a.fast||a.type===p.ARRAY&&!a.fast)&&(r=!0)}return{type:p.ARRAY,items:n,fast:!r}},nn=(e,t)=>{let n=[],r=!1,i=null,o=Object.entries(e),a=0;if(e.$partial!==void 0){if(typeof e.$partial!="string")throw new b("$partial value must be a string");if(e.$partial.trim()==="")throw new b("$partial value cannot be an empty string");let l=["$if","$elif","$else","$for"],s=[];for(let[m]of o)for(let _ of l)if(m===_||m.startsWith(_+" ")||m.startsWith(_+"#")){s.push(_);break}if(s.length>0)throw new b(`Cannot use $partial with ${s.join(", ")} at the same level. Wrap $partial in a parent object if you need conditionals.`);let{$partial:u,$when:f,...d}=e,h={},g=!1;for(let[m,_]of Object.entries(d)){let v=m;(m.startsWith("\\$")||m.startsWith("$$"))&&(v=m.slice(1)),h[v]=_,g=!0}let y=null;if(g&&(y=S(h,t),y.type===p.OBJECT)){let m=!1;for(let _ of y.properties)if(_.value.type===p.VARIABLE||_.value.type===p.INTERPOLATION||_.value.type===p.FUNCTION||_.value.type===p.CONDITIONAL||_.value.type===p.OBJECT&&!_.value.fast||_.value.type===p.ARRAY&&!_.value.fast){m=!0;break}m&&(y.fast=!1)}let I={type:p.PARTIAL,name:u,data:y};if(f!==void 0){let m;if(typeof f=="string"){if(f.trim()==="")throw new b("Empty condition expression after '$when'");m=x(f,t)}else m={type:p.LITERAL,value:f};I.whenCondition=m}return I}for(let[l,s]of o)if(l==="$when"){if(i!==null)throw new b("Multiple '$when' directives on the same object are not allowed");if(s==null)throw new b("Missing condition expression after '$when'");let u=typeof s=="string"?s:JSON.stringify(s);if(u.trim()==="")throw new b("Empty condition expression after '$when'");i=x(u,t),r=!0}else if(l.startsWith("$when#")||l.startsWith("$when "))throw new b("'$when' does not support ID syntax or inline conditions - use '$when' as a property");for(;a<o.length;){let[l,s]=o[a];if(l==="$when"){a++;continue}if(l==="$each")throw new b("$each can only be used inside arrays");if(l.startsWith("$if ")||l.match(/^\$if#\w+\s/)||l.match(/^\$if\s+\w+.*:$/)){let u=rn(o,a,t);n.push({key:l,value:u.node}),r=!0,a=u.nextIndex}else if(/^\$for(?::\w+)?\s/.test(l)){let u=l.match(/^\$for(?::(\w+))?\s/)?.[1]||"",f=u?`:${u}`:"";throw new b(`$for loops must be inside arrays - use '- $for${f} item in items:' instead of '$for${f} item in items:'. For cleaner object generation syntax, consider using $each.`)}else{if(l.startsWith("$elif ")||l.startsWith("$else"))throw new b(`'${l.split(" ")[0]}' without matching '$if'`);if(l==="$if"||l==="$if:")throw new b("Missing condition expression after '$if'");{let u=S(s,t);(u.type===p.FUNCTION||u.type===p.CONDITIONAL||u.type===p.PARTIAL||u.type===p.OBJECT&&!u.fast||u.type===p.ARRAY&&!u.fast)&&(r=!0);let f=me(l,t),d={key:l,value:u};(f.type!==p.LITERAL||f.value!==l)&&(d.parsedKey=f),n.push(d),a++}}}let c={type:p.OBJECT,properties:n,fast:!r};return i&&(c.whenCondition=i),c},rn=(e,t,n={})=>{let r=[],i=[],o=t,[a,c]=e[o],l=null,s;if(a.startsWith("$if#")){let f=a.match(/^\$if#(\w+)\s+(.+)$/);if(f)l=f[1],s=f[2];else throw new b(`Invalid conditional syntax: ${a}`)}else s=a.substring(4),s.endsWith(":")&&(s=s.slice(0,-1).trim());ge(s);let u=x(s,n);for(r.push(u),i.push(S(c,n)),o++;o<e.length;){let[f,d]=e[o],h=!1,g;if(l?f.startsWith(`$elif#${l} `)?(g=f.substring(`$elif#${l} `.length),g.endsWith(":")&&(g=g.slice(0,-1).trim()),h=!0):(f===`$else#${l}`||f===`$else#${l}:`)&&(h=!0,g=null):f.startsWith("$elif ")?(g=f.substring(6),g.endsWith(":")&&(g=g.slice(0,-1).trim()),h=!0):(f==="$else"||f==="$else:")&&(h=!0,g=null),h){if(g===null)r.push(null);else{ge(g);let y=x(g,n);r.push(y)}if(i.push(S(d,n)),o++,g===null)break}else break}return{node:{type:p.CONDITIONAL,conditions:r,bodies:i,id:l},nextIndex:o}},x=(e,t={})=>{if(e=e.trim(),e.startsWith("(")&&e.endsWith(")")){let s=e.slice(1,-1),u=0,f=!0;for(let d=0;d<s.length;d++)if(s[d]==="("?u++:s[d]===")"&&u--,u<0){f=!1;break}if(f&&u===0)return x(s,t)}let n=D(e,"||");if(n!==-1)return{type:p.BINARY,op:A.OR,left:x(e.substring(0,n).trim(),t),right:x(e.substring(n+2).trim(),t)};let r=D(e,"&&");if(r!==-1)return{type:p.BINARY,op:A.AND,left:x(e.substring(0,r).trim(),t),right:x(e.substring(r+2).trim(),t)};let i=[{op:">=",type:A.GTE},{op:"<=",type:A.LTE},{op:"==",type:A.EQ},{op:"!=",type:A.NEQ},{op:">",type:A.GT},{op:"<",type:A.LT},{op:" in ",type:A.IN}];for(let{op:s,type:u}of i){let f=D(e,s);if(f!==-1)return{type:p.BINARY,op:u,left:x(e.substring(0,f).trim(),t),right:x(e.substring(f+s.length).trim(),t)}}let o=-1,a=null,c=[{op:" + ",type:A.ADD},{op:" - ",type:A.SUBTRACT}];for(let{op:s,type:u}of c){let f=0;for(;f<e.length;){let d=D(e.substring(f),s);if(d===-1)break;let h=f+d;h>o&&(o=h,a={op:s,type:u}),f=h+s.length}}if(o!==-1&&a)return{type:p.BINARY,op:a.type,left:x(e.substring(0,o).trim(),t),right:x(e.substring(o+a.op.length).trim(),t)};let l=[" * "," / "," % "];for(let s of l)if(D(e,s)!==-1)throw new b(`Arithmetic operations are not allowed in conditionals: "${s}"`);return e.startsWith("!")?{type:p.UNARY,op:j.NOT,operand:x(e.substring(1).trim(),t)}:He(e,t)},D=(e,t)=>{let n=0,r=0;for(;r<=e.length-t.length;){if(e[r]==="(")n++;else if(e[r]===")")n--;else if(n===0&&e.substring(r,r+t.length)===t)return r;r++}return-1},on=e=>{if(e=e.trim(),e==="true")return{type:p.LITERAL,value:!0};if(e==="false")return{type:p.LITERAL,value:!1};if(e==="null")return{type:p.LITERAL,value:null};if(e.startsWith('"')&&e.endsWith('"')||e.startsWith("'")&&e.endsWith("'"))return{type:p.LITERAL,value:e.slice(1,-1)};if(e==='""'||e==="''")return{type:p.LITERAL,value:""};if(e==="{}")return{type:p.LITERAL,value:{}};if(e==="[]")return{type:p.LITERAL,value:[]};let t=Number(e);return!isNaN(t)&&isFinite(t)?{type:p.LITERAL,value:t}:{type:p.VARIABLE,path:e}},He=(e,t)=>{let n=e.trim();if(n.match(/^(\w+)\((.*)\)$/))return K(n,t);let i=on(n);if(i.type===p.LITERAL)return i;if(/^[a-zA-Z_$][\w.$]*$/.test(n))return{type:p.VARIABLE,path:n};try{return K(n,t)}catch(o){if(o.message&&o.message.includes("not supported"))return i;throw o}},sn=(e,t=!1)=>{let n=t?"$each":"$for";try{Te(e)}catch(r){if(r instanceof b&&t){let i=r.message.replace(/\$for/g,"$each");throw new b(i.replace("Parse Error: ",""))}throw r}},Ye=(e,t,n,r=!1)=>{let i=/^\$for(?::(\w+))?\s+(.+)$/,o=e.match(i);if(!o){let I=r?"$each":"$for";throw new b(`Invalid loop syntax (got: '${e.replace("$for",I)}')`)}let a=o[1],c=o[2].trim();sn(c,r);let l=c.match(/^(.+?)\s+in\s+(.+)$/);if(!l){let I=r?"$each":"$for";throw new b(`Invalid loop syntax - missing 'in' keyword (got: '${I} ${c}')`)}let s=l[1].trim(),u=l[2].trim(),f,d=null;if(s.includes(",")){let I=s.split(",").map(m=>m.trim());if(I.length!==2)throw new b(`Invalid loop variables: ${s}. Expected format: "item" or "item, index"`);f=I[0],d=I[1]}else f=s;let h=["this","undefined","null","true","false"];if(h.includes(f))throw new b(`Reserved variable name: ${f}`);if(d&&h.includes(d))throw new b(`Reserved variable name: ${d}`);let g=He(u,n),y=S(t,n);return{type:p.LOOP,itemVar:f,indexVar:d,iterable:g,body:y,flatten:a!=="nested"}};var an=(e,t={})=>{let{functions:n={}}=t;return S(e,n)},ee=an;var be={};at(be,{now:()=>cn});var cn=()=>Date.now();var ln=(e,t,n={})=>{let{functions:r={},partials:i={}}=n,o={...be,...r},a=ee(e,{functions:o}),c={};for(let[l,s]of Object.entries(i))c[l]=ee(s,{functions:o});return ye(a,t,{functions:o,partials:c})},N=ln;var Ie={objectValues:e=>Object.entries(e).map(([t,n])=>({id:t,...n}))},un=()=>({elements:[{id:"story",type:"container",x:0,y:0,children:[]}],animations:[],audio:[],global:{}}),fn=(e,{presentationState:t,resources:n})=>{let{elements:r}=e;if(t.base){let i=r.find(o=>o.id==="story");if(!i)return e;if(t.base.resourceId){let o=n?.layouts[t.base.resourceId];o&&i.children.unshift({id:"base",type:"container",children:o.elements})}}return e},dn=(e,{presentationState:t,previousPresentationState:n,resources:r={},variables:i,autoMode:o,skipMode:a,currentLocalizationPackageId:c})=>{let{elements:l}=e,s=e.animations||[];if(t.background){let u=l.find(f=>f.id==="story");if(!u)return e;if(t.background.resourceId){let{images:f={},videos:d={}}=r,h=f[t.background.resourceId]||d[t.background.resourceId];if(h){let g=d[t.background.resourceId]!==void 0,y={id:`bg-cg-${t.background.resourceId}`,type:g?"video":"sprite",x:0,y:0,src:h.fileId,width:h.width,height:h.height};g&&(y.loop=h.loop??!1,y.volume=h.volume??500),u.children.push(y)}}if(t.background.resourceId){let{layouts:f={}}=r,d=f[t.background.resourceId];d&&u.children.push({id:`bg-cg-${t.background.resourceId}`,type:"container",children:d.elements})}if(t.background.animations){if(t.background.animations.in){let f=t.background.animations.in.resourceId,d=r?.tweens[f];d&&s.push({id:"bg-cg-animation-in",type:"tween",targetId:`bg-cg-${t.background.resourceId}`,properties:d.properties})}if(t.background.animations.out){let f=t.background.animations.out.resourceId,d=n?.background?.resourceId,h=r?.tweens[f];h&&d&&s.push({id:"bg-cg-animation-out",type:"tween",targetId:`bg-cg-${d}`,properties:h.properties})}if(t.background.animations.update){let f=t.background.animations.update.resourceId,d=r?.tweens[f];d&&s.push({id:"bg-cg-animation-update",type:"tween",targetId:`bg-cg-${t.background.resourceId}`,properties:d.properties})}}}return e},pn=(e,{presentationState:t,resources:n})=>{let{elements:r}=e,i=e.animations||[];if(t.character&&n){let o=r.find(c=>c.id==="story");if(!o)return e;let a=t.character.items||[];for(let c of a){let{transformId:l,sprites:s}=c;if(c.animations&&c.animations.out&&!s&&!l){let h=c.animations.out.resourceId,g=n?.tweens[h];if(g){let y={id:"character-animation-out",type:"tween",targetId:`character-container-${c.id}`,properties:g.properties};i.push(y)}continue}if(!s||s.length===0||!l){console.warn("Character item missing sprites or transformId:",c);continue}let u=s.map(({resourceId:h})=>h),f=n.transforms[l];if(!f){console.warn("Transform not found:",l);continue}let d={type:"container",id:`character-container-${c.id}`,x:c.x??f.x,y:c.y??f.y,anchorX:f.anchorX,anchorY:f.anchorY,rotation:f.rotation,scaleX:f.scaleX,scaleY:f.scaleY,children:[]};for(let h of s){let g=n.images[h.resourceId];if(!g){console.warn(`Image resource not found: ${h.resourceId}`);continue}d.children.push({type:"sprite",id:`${c.id}-${h.id}`,src:g.fileId,width:g.width,height:g.height,x:0,y:0})}if(o.children.push(d),c.animations){if(c.animations.in){let h=c.animations.in.resourceId,g=n?.tweens[h];g&&i.push({id:"character-animation-in",type:"tween",targetId:`character-container-${c.id}`,properties:g.properties})}if(c.animations.update){let h=c.animations.update.resourceId,g=n?.tweens[h];if(g){let y={id:"character-animation-update",type:"tween",targetId:`character-container-${c.id}`,properties:g.properties};i.push(y)}}}}}return e},hn=(e,{presentationState:t,resources:n})=>{let{elements:r}=e,i=e.animations||[];if(t.visual&&n){let o=r.find(c=>c.id==="story");if(!o)return e;let a=t.visual.items;for(let c of a){if(c.resourceId){let{images:l={},videos:s={}}=n,u=l[c.resourceId]||s[c.resourceId];if(u){let f=s[c.resourceId]!==void 0,d=n.transforms[c.transformId],h={id:`visual-${c.id}`,type:f?"video":"sprite",src:u.fileId,width:u.width,height:u.height,x:d.x,y:d.y,anchorX:d.anchorX,anchorY:d.anchorY,rotation:d.rotation,scaleX:d.scaleX,scaleY:d.scaleY};f&&(h.loop=u.loop??!1,h.volume=u.volume??500),o.children.push(h)}}if(c.resourceId){let{layouts:l={}}=n,s=l[c.resourceId];if(s){let u=n.transforms[c.transformId];o.children.push({id:`visual-${c.id}`,type:"container",children:s.elements,x:u.x,y:u.y,anchorX:u.anchorX,anchorY:u.anchorY,rotation:u.rotation,scaleX:u.scaleX,scaleY:u.scaleY})}}if(c.animations){if(c.animations.in){let l=c.animations.in.resourceId||c.animations.in,s=n?.tweens[l];s&&i.push({id:`${c.id}-animation`,type:"tween",targetId:`visual-${c.id}`,properties:s.properties})}if(c.animations.out){let l=c.animations.out.resourceId||c.animations.out,s=n?.tweens[l];s&&i.push({id:`${c.id}-animation-2`,type:"tween",targetId:`visual-${c.id}`,properties:s.properties})}}}}return e},gn=(e,{presentationState:t,resources:n={},dialogueUIHidden:r,autoMode:i,skipMode:o,skipOnlyViewedLines:a,l10n:c,variables:l})=>{let{elements:s}=e;if(!t.dialogue||r)return e;let u=s.find(f=>f.id==="story");if(!u)return e;if(t.dialogue.gui&&t.dialogue.gui.resourceId){let{layouts:f={}}=n,d=f[t.dialogue.gui.resourceId];if(d){let h;t.dialogue.characterId&&(h=n.characters[t.dialogue.characterId]),t.dialogue.character&&(h={...h,name:t.dialogue.character.name});let g={elements:d.elements},y={variables:l,autoMode:i,skipMode:o,skipOnlyViewedLines:a,dialogue:{character:{name:h?.name||""},content:t.dialogue?.content||[{text:""}],lines:t.dialogue?.lines||[]},l10n:c},I=N(g,y,{functions:Ie});I=N(I,{l10n:c});let m=I?.elements;if(Array.isArray(m))for(let _ of m)u.children.push(structuredClone(_));else m&&u.children.push(structuredClone(m))}}return e},yn=(e,{presentationState:t,resources:n})=>{let{elements:r}=e;if(t.choice&&n){let i=r.find(s=>s.id==="story");if(!i)return e;let o=n?.layouts[t.choice.resourceId];if(!o||!o.elements)return e;let a={elements:o.elements},l=N(a,{choice:{items:t.choice.items}})?.elements;if(Array.isArray(l))for(let s of l)i.children.push(structuredClone(s));else l&&i.children.push(structuredClone(l))}return e},mn=(e,{presentationState:t,resources:n})=>{if(t.keyboard?.resourceId){let r=n?.keyboards?.[t.keyboard.resourceId];r&&(e.global.keyboard=r)}return e},bn=(e,{presentationState:t,resources:n})=>{let{elements:r,audio:i}=e;if(t.bgm&&n){if(!r.find(c=>c.id==="story"))return e;let a=n.sounds[t.bgm.resourceId];if(!a)return e;i.push({id:"bgm",type:"sound",src:a.fileId,loop:t.bgm.loop??!0,volume:t.bgm.volume??500,delay:t.bgm.delay??null})}return e},In=(e,{presentationState:t,resources:n})=>{let{audio:r}=e;if(t.sfx&&n){let i=t.sfx.items;for(let o of i){let a=n.sounds?.[o.resourceId];a&&r.push({id:o.id,type:"sound",src:a.fileId,loop:o.loop??a.loop??!0,volume:o.volume??a.volume??500,delay:o.delay??a.delay??null})}}return e},_n=(e,{presentationState:t,resources:n})=>{let{audio:r}=e;if(!t?.voice)return e;let{fileId:i,volume:o,loop:a}=t.voice;return r.push({id:`voice-${i}`,type:"sound",src:i,volume:o??500,loop:a??!1}),e},wn=(e,{presentationState:t,resources:n={},variables:r,autoMode:i,skipMode:o,currentLocalizationPackageId:a})=>{let{elements:c}=e,l=e.animations||[];if(t.layout){let s=c.find(y=>y.id==="story");if(!s)return e;let u=n.layouts[t.layout.resourceId];if(!u)return e;Array.isArray(u.transitions)&&u.transitions.forEach(y=>{l.push(y)});let f={id:`layout-${t.layout.resourceId}`,type:"container",x:0,y:0,children:u.elements||[]},h=N(f,{variables:r,autoMode:i,skipMode:o,currentLocalizationPackageId:a},{functions:Ie});h=N(h,{i18n:{}});let g=y=>{let I={...y};return y.src&&y.src.startsWith("file:")&&(I.src=y.src),y.children&&Array.isArray(y.children)&&(I.children=y.children.map(g)),I};s.children.push(g(h))}return e},An=(e,{resources:t={},variables:n,autoMode:r,skipMode:i,currentLocalizationPackageId:o,layeredViews:a=[],dialogueHistory:c=[]})=>{let{elements:l}=e,s=e.animations||[];return a&&a.length>0&&a.forEach((u,f)=>{let d=t.layouts[u.resourceId];if(!d){console.warn(`LayeredView layout not found: ${u.resourceId}`);return}Array.isArray(d.transitions)&&d.transitions.forEach(m=>{s.push(m)});let h={id:`layeredView-${f}`,type:"container",x:0,y:0,children:d.elements||[]},g=c.map(m=>{let _=t.characters?.[m.characterId];return{...m,characterName:_?.name||""}}),y={variables:n,autoMode:r,skipMode:i,currentLocalizationPackageId:o,historyDialogue:g,characters:t.characters||{}},I=N(h,y,{functions:Ie});I=N(I,{i18n:{}}),l.push(I)}),e},Je=e=>Z(un,[fn,dn,pn,hn,gn,yn,mn,wn,bn,In,_n,An])(e);var En=e=>{let{global:{currentLocalizationPackageId:t,saveSlots:n},projectData:r}=e,i={sceneId:r.story.initialSceneId,sectionId:r.story.scenes[r.story.initialSceneId].initialSectionId,lineId:r.story.scenes[r.story.initialSceneId].sections[r.story.scenes[r.story.initialSceneId].initialSectionId].lines[0].id},o={projectData:r,global:{autoplayDelay:1e3,isLineCompleted:!1,pendingEffects:[],autoMode:!1,skipMode:!1,skipOnlyViewedLines:!0,dialogueUIHidden:!1,isDialogueHistoryShowing:!1,currentLocalizationPackageId:t,viewedRegistry:{sections:[],resources:[]},nextLineConfig:{manual:{enabled:!0,requireLineCompleted:!1},auto:{enabled:!1}},saveSlots:n,layeredViews:[]},contexts:[{currentPointerMode:"read",pointers:{read:i,history:{sectionId:void 0,lineId:void 0,historySequenceIndex:void 0}},historySequence:[{sectionId:"..."}],configuration:{},views:[],bgm:{resourceId:void 0},variables:{}}]};return console.log("Initial State:",o),o},vn=({state:e})=>e.global.layeredViews||[],xn=({state:e})=>e.global.pendingEffects,Xe=({state:e})=>e.global.skipMode,Ln=({state:e})=>e.global.skipOnlyViewedLines,$n=({state:e})=>e.global.autoMode,qe=({state:e})=>e.global.dialogueUIHidden,kn=({state:e})=>e.global.autoplayDelay,_e=({state:e})=>{let t=e.contexts[e.contexts.length-1];if(!t)return[];let{sectionId:n,lineId:r}=t.pointers.read,i=C({state:e},{sectionId:n});if(!i?.lines||!Array.isArray(i.lines))return[];let o=i.lines.findIndex(l=>l.id===r);return i.lines.slice(0,o+1).filter(l=>l.actions?.dialogue).map(l=>{let s=l.actions.dialogue,u="";return s.characterId&&(u=e.projectData.resources?.characters?.[s.characterId]?.name||""),{content:s.content,characterId:s.characterId,characterName:u}})},Cn=({state:e})=>e.global.currentLocalizationPackageId,Ge=({state:e},t)=>{let{sectionId:n,lineId:r}=t,i=e.global.viewedRegistry.sections.find(l=>l.sectionId===n);if(!i)return!1;if(i.lastLineId===void 0||r===void 0)return!0;let o=C({state:e},{sectionId:n});if(!o||!o.lines||!Array.isArray(o.lines))return!1;let a=o.lines.findIndex(l=>l.id===i.lastLineId),c=o.lines.findIndex(l=>l.id===r);return a===-1||c===-1?i.lastLineId===r:c<a},On=({state:e},t)=>{let{resourceId:n}=t;return!!e.global.viewedRegistry.resources.find(i=>i.resourceId===n)},Rn=({state:e})=>e.global.nextLineConfig,Nn=({state:e})=>e.global.saveSlots,Sn=({state:e},t)=>{let{slotKey:n}=t;return e.global.saveSlots[n]},W=({state:e})=>{let t=e.contexts[e.contexts.length-1];if(!t)return;let n=t.pointers?.[t.currentPointerMode];return{currentPointerMode:t.currentPointerMode,pointer:n}},C=({state:e},t)=>{let{sectionId:n}=t,r=e.projectData?.story?.scenes||{};for(let i in r){let o=r[i];if(o.sections&&o.sections[n])return o.sections[n]}},Pn=({state:e})=>{let t=W({state:e});if(!t?.pointer)return;let{sectionId:n,lineId:r}=t.pointer,i=C({state:e},{sectionId:n});if(!(!i?.lines||!Array.isArray(i.lines)))return i.lines.find(o=>o.id===r)},Qe=({state:e})=>{let{sectionId:t,lineId:n}=W({state:e}).pointer,i=C({state:e},{sectionId:t})?.lines||[],o=i.findIndex(s=>s.id===n),a=i.slice(0,o+1);console.log("currentLines",a);let c=a.map(s=>{let u=s.actions||{},f={};return Object.keys(u).forEach(d=>{f[d]=u[d]}),f});return he(c)},Tn=({state:e})=>{let{sectionId:t,lineId:n}=W({state:e}).pointer,i=C({state:e},{sectionId:t})?.lines||[],o=i.findIndex(l=>l.id===n);if(o<=0)return null;let c=i.slice(0,o).map(l=>{let s=l.actions||{},u={};return Object.keys(s).forEach(f=>{u[f]=s[f]}),u});return he(c)},Vn=({state:e})=>{let t=Qe({state:e}),n=Tn({state:e});console.log("presentationState",t);let r=Je({presentationState:t,previousPresentationState:n,resources:e.projectData.resources,l10n:e.projectData.l10n.packages[e.global.currentLocalizationPackageId],dialogueUIHidden:e.global.dialogueUIHidden,autoMode:e.global.autoMode,skipMode:e.global.skipMode,skipOnlyViewedLines:e.global.skipOnlyViewedLines,layeredViews:e.global.layeredViews,dialogueHistory:_e({state:e})});return console.log("renderState",r),r},Dn=({state:e},t)=>(e.global.layeredViews.push(t),e.global.pendingEffects.push({name:"render"}),e),Mn=({state:e})=>(e.global.layeredViews.pop(),e.global.pendingEffects.push({name:"render"}),e),Bn=({state:e},t)=>(e.global.layeredViews.length>0&&(e.global.layeredViews[e.global.layeredViews.length-1]=t),e.global.pendingEffects.push({name:"render"}),e),Fn=({state:e})=>(e.global.layeredViews=[],e.global.pendingEffects.push({name:"render"}),e),Ze=({state:e})=>(e.global.skipMode&&(e.global.skipMode=!1,e.global.pendingEffects.push({name:"clearSkipNextTimer"})),e.global.autoMode=!0,e.global.pendingEffects.push({name:"clearAutoNextTimer"}),e.global.pendingEffects.push({name:"startAutoNextTimer",payload:{delay:e.global.autoplayDelay}}),e.global.pendingEffects.push({name:"render"}),e),we=({state:e})=>(e.global.autoMode=!1,e.global.pendingEffects.push({name:"clearAutoNextTimer"}),e.global.pendingEffects.push({name:"render"}),e),Wn=({state:e})=>(e.global.autoMode?we({state:e}):Ze({state:e}),e),et=({state:e})=>(e.global.autoMode&&(e.global.autoMode=!1,e.global.pendingEffects.push({name:"clearAutoNextTimer"})),e.global.skipMode=!0,e.global.pendingEffects.push({name:"clearSkipNextTimer"}),e.global.pendingEffects.push({name:"startSkipNextTimer"}),e.global.pendingEffects.push({name:"render"}),e),te=({state:e})=>(e.global.skipMode=!1,e.global.pendingEffects.push({name:"clearSkipNextTimer"}),e.global.pendingEffects.push({name:"render"}),e),Un=({state:e})=>(Xe({state:e})?te({state:e}):et({state:e}),e),zn=({state:e},t)=>{let{skipOnlyViewedLines:n}=t;return e.global.skipOnlyViewedLines=n,e.global.pendingEffects.push({name:"render"}),e},jn=({state:e})=>(e.global.skipOnlyViewedLines=!e.global.skipOnlyViewedLines,e.global.pendingEffects.push({name:"render"}),e),tt=({state:e})=>(e.global.dialogueUIHidden=!1,e.global.pendingEffects.push({name:"render"}),e),nt=({state:e})=>(e.global.dialogueUIHidden=!0,e.global.pendingEffects.push({name:"render"}),e),Kn=({state:e})=>(qe({state:e})?tt({state:e}):nt({state:e}),e),Yn=({state:e})=>{let t=_e({state:e});return e.global.isDialogueHistoryShowing=!0,e.global.pendingEffects.push({name:"render"}),e},Hn=({state:e})=>(e.global.isDialogueHistoryShowing=!1,e.global.pendingEffects.push({name:"render"}),e),Jn=({state:e},t)=>{let{localizationPackageId:n}=t;return e.global.currentLocalizationPackageId=n,e.global.pendingEffects.push({name:"render"}),e},Xn=({state:e})=>(e.global.pendingEffects=[],e),qn=({state:e},t)=>(e.global.pendingEffects.push(t),e),rt=({state:e},t)=>{let{sectionId:n,lineId:r}=t,i=e.global.viewedRegistry.sections.find(o=>o.sectionId===n);if(i){let o=C({state:e},{sectionId:n});if(o?.lines&&i.lastLineId!==void 0){let a=o.lines.findIndex(l=>l.id===i.lastLineId),c=o.lines.findIndex(l=>l.id===r);(a===-1||c>a)&&(i.lastLineId=r)}else i.lastLineId=r}else e.global.viewedRegistry.sections.push({sectionId:n,lastLineId:r});return e.global.pendingEffects.push({name:"render"}),e},Gn=({state:e},t)=>{let{resourceId:n}=t;return e.global.viewedRegistry.resources.find(i=>i.resourceId===n)||e.global.viewedRegistry.resources.push({resourceId:n}),e.global.pendingEffects.push({name:"render"}),e},Qn=({state:e},t)=>{let{manual:n,auto:r}=t;return n&&r?e.global.nextLineConfig={manual:n,auto:r}:(n&&(e.global.nextLineConfig.manual=n),r&&(e.global.nextLineConfig.auto=r)),e.global.pendingEffects.push({name:"render"}),e},Zn=({state:e},{delay:t})=>(e.global.autoplayDelay=t,e.global.autoMode&&(e.global.pendingEffects.push({name:"clearAutoNextTimer"}),e.global.pendingEffects.push({name:"startAutoNextTimer",payload:{delay:e.global.autoplayDelay}})),e.global.pendingEffects.push({name:"render"}),e),er=({state:e},t)=>{let{slot:n,thumbnailImage:r}=t,i=String(n),o={contexts:[...e.contexts],viewedRegistry:e.global.viewedRegistry},a={slotKey:i,date:Date.now(),image:r,state:o};return e.global.saveSlots[i]=a,e.global.pendingEffects.push({name:"saveSlots",payload:{saveSlots:{...e.global.saveSlots}}},{name:"render"}),e},tr=({state:e},t)=>{let{slot:n}=t,r=String(n),i=e.global.saveSlots[r];return i&&(e.global.viewedRegistry=i.state.viewedRegistry,e.contexts=i.state.contexts,e.global.pendingEffects.push({name:"render"})),e},nr=({state:e},t)=>{let{projectData:n}=t;return e.projectData=n,e.global.pendingEffects.push({name:"render"}),e},rr=({state:e},t)=>{let{sectionId:n,lineId:r}=t;if(!r)return console.warn("jumpToLine requires lineId parameter"),e;let i=e.contexts[e.contexts.length-1];if(!i)return console.warn("No context available for jumpToLine"),e;let o=n||i.pointers.read?.sectionId;if(n&&!C({state:e},{sectionId:n}))return console.warn(`Section not found: ${n}`),e;let a=C({state:e},{sectionId:o});return!a?.lines||!Array.isArray(a.lines)?(console.warn(`Section ${o} has no lines`),e):a.lines.find(l=>l.id===r)?(i.pointers.read={sectionId:o,lineId:r},e.global.isLineCompleted=!1,e.global.pendingEffects.push({name:"render"}),e.global.pendingEffects.push({name:"handleLineActions"}),e):(console.warn(`Line not found: ${r} in section ${o}`),e)},ir=({state:e},t)=>{let{item:n}=t,r=e.contexts[e.contexts.length-1];return r&&r.historySequence&&r.historySequence.push({sectionId:n.sectionId}),e.global.pendingEffects.push({name:"render"}),e},or=({state:e})=>{if(e.global.nextLineConfig?.auto?.enabled!==!0||e.global.nextLineConfig?.auto?.trigger!=="fromComplete")return e;let t=W({state:e})?.pointer,n=t?.sectionId,i=C({state:e},{sectionId:n})?.lines||[],a=i.findIndex(c=>c.id===t?.lineId)+1;if(a<i.length){let c=i[a],l=e.contexts[e.contexts.length-1];l&&(l.pointers.read={sectionId:n,lineId:c.id}),e.global.isLineCompleted=!1,e.global.nextLineConfig.auto={},e.global.pendingEffects.push({name:"render"}),e.global.pendingEffects.push({name:"handleLineActions"})}return e},sr=({state:e})=>{if(!e.global.nextLineConfig.manual.enabled)return e;let t=W({state:e})?.pointer,n=t?.sectionId,i=C({state:e},{sectionId:n})?.lines||[],a=i.findIndex(c=>c.id===t?.lineId)+1;if(a<i.length){let c=i[a];e.global.skipMode&&e.global.skipOnlyViewedLines&&(Ge({state:e},{sectionId:n,lineId:c.id})||te({state:e}));let l=e.contexts[e.contexts.length-1];if(l){let s=l.pointers.read.lineId;s&&n&&rt({state:e},{sectionId:n,lineId:s}),l.pointers.read={sectionId:n,lineId:c.id}}e.global.isLineCompleted=!1,e.global.pendingEffects.push({name:"render"}),e.global.pendingEffects.push({name:"handleLineActions"})}else e.global.autoMode&&we({state:e}),e.global.skipMode&&te({state:e});return console.log("state",e),e},ar=({state:e})=>(e.global.isLineCompleted=!0,e.global.pendingEffects.push({name:"render"}),e),cr=({state:e},t)=>{let{sectionId:n}=t,r=C({state:e},{sectionId:n});if(!r||!r.lines||r.lines.length===0)return e;let i=r.lines,o=e.contexts[e.contexts.length-1];if(!o||!o.pointers)return e;let a=o.pointers.history||o.pointers.read;if(o.currentPointerMode!=="history"||!o.pointers.history){if(!a)return e;o.currentPointerMode="history",o.pointers.history={sectionId:n,lineId:a?.lineId};let u=i.findIndex(f=>f.id===a.lineId)-1;if(u>=0&&u<i.length){let f=i[u];o.pointers.history={sectionId:n,lineId:f.id}}return e.global.pendingEffects.push({name:"render"}),e}let l=i.findIndex(s=>s.id===o.pointers.history.lineId)-1;if(l>=0&&l<i.length){let s=i[l];o.pointers.history={sectionId:n,lineId:s.id},e.global.pendingEffects.push({name:"render"})}return e},lr=({state:e},t)=>{let{sectionId:n}=t,r=C({state:e},{sectionId:n});if(!r)return console.warn(`Section not found: ${n}`),e;let i=r.lines?.[0];if(!i)return console.warn(`Section ${n} has no lines`),e;let o=e.contexts[e.contexts.length-1];return o&&(o.pointers.read={sectionId:n,lineId:i.id}),e.global.isLineCompleted=!1,e.global.pendingEffects.push({name:"render"}),e.global.pendingEffects.push({name:"handleLineActions"}),e},it=e=>{let t=En(e);return Pe(t,{selectPendingEffects:xn,selectSkipMode:Xe,selectSkipOnlyViewedLines:Ln,selectAutoMode:$n,selectDialogueUIHidden:qe,selectDialogueHistory:_e,selectCurrentLocalizationPackageId:Cn,selectIsLineViewed:Ge,selectIsResourceViewed:On,selectNextLineConfig:Rn,selectSaveSlots:Nn,selectSaveSlot:Sn,selectCurrentPointer:W,selectSection:C,selectCurrentLine:Pn,selectPresentationState:Qe,selectAutoplayDelay:kn,selectRenderState:Vn,selectLayeredViews:vn,startAutoMode:Ze,stopAutoMode:we,toggleAutoMode:Wn,startSkipMode:et,stopSkipMode:te,toggleSkipMode:Un,setSkipOnlyViewedLines:zn,toggleSkipOnlyViewedLines:jn,showDialogueUI:tt,hideDialogueUI:nt,toggleDialogueUI:Kn,showDialogueHistory:Yn,hideDialogueHistory:Hn,setCurrentLocalizationPackageId:Jn,clearPendingEffects:Xn,appendPendingEffect:qn,addViewedLine:rt,addViewedResource:Gn,setNextLineConfig:Qn,saveSaveSlot:er,loadSaveSlot:tr,setAutoplayDelay:Zn,updateProjectData:nr,sectionTransition:lr,jumpToLine:rr,addToHistorySequence:ir,nextLine:sr,nextLineFromCompleted:or,markLineCompleted:ar,prevLine:cr,pushLayeredView:Dn,popLayeredView:Mn,replaceLastLayeredView:Bn,clearLayeredViews:Fn},{transformActionFirstArgument:r=>({state:r}),transformSelectorFirstArgument:r=>({state:r})})};function Ae(e){let t,{handlePendingEffects:n}=e,r=({initialState:u})=>{t=it(u),t.appendPendingEffect({name:"render"}),s(),n(t.selectPendingEffects()),t.clearPendingEffects()},i=()=>t.selectPresentationState(),o=()=>t.selectRenderState(),a=()=>t.selectSaveSlots(),c=(u,f)=>{t[u]&&(t[u](f),n(t.selectPendingEffects()),t.clearPendingEffects())},l=u=>{Object.entries(u).forEach(([f,d])=>{c(f,d)})},s=()=>{let u=t.selectCurrentLine();u&&u.actions&&l(u.actions)};return{init:r,handleAction:c,handleActions:l,selectRenderState:o,selectPresentationState:i,selectSaveSlots:a,handleLineActions:s}}var ot=()=>{let e=0,t=null;return{getElapsed:()=>e,setElapsed:n=>{e=n},addElapsed:n=>{e+=n},getCallback:()=>t,setCallback:n=>{t=n}}},ur=({engine:e,routeGraphics:t},n)=>{let r=e.selectRenderState();t.render(r)},fr=({engine:e},t)=>{e.handleLineActions()},dr=({engine:e,ticker:t,autoTimer:n},r)=>{let i=n.getCallback();i&&t.remove(i),n.setElapsed(0);let o=a=>{n.addElapsed(a.deltaMS);let c=r.delay??1e3;n.getElapsed()>=c&&(n.setElapsed(0),e.handleAction("nextLine",{}))};n.setCallback(o),t.add(o)},pr=({ticker:e,autoTimer:t},n)=>{let r=t.getCallback();r&&(e.remove(r),t.setCallback(null)),t.setElapsed(0)},hr=({engine:e,ticker:t,skipTimer:n},r)=>{let i=n.getCallback();i&&t.remove(i),n.setElapsed(0);let o=a=>{n.addElapsed(a.deltaMS),n.getElapsed()>=30&&(n.setElapsed(0),e.handleAction("nextLine",{}))};n.setCallback(o),t.add(o)},gr=({ticker:e,skipTimer:t},n)=>{let r=t.getCallback();r&&(e.remove(r),t.setCallback(null)),t.setElapsed(0)},yr=({},e)=>{localStorage.setItem("saveSlots",JSON.stringify(e.saveSlots))},mr={render:ur,saveSlots:yr,handleLineActions:fr,startAutoNextTimer:dr,clearAutoNextTimer:pr,startSkipNextTimer:hr,clearSkipNextTimer:gr},br=({getEngine:e,routeGraphics:t,ticker:n})=>{let r=ot(),i=ot();return async o=>{let a=e(),c=o.reduce((u,f)=>(u[f.name]=f,u),{}),l=Object.values(c),s={engine:a,routeGraphics:t,ticker:n,autoTimer:r,skipTimer:i};for(let u of l){let f=mr[u.name];f&&f(s,u.payload)}}},Ir=br;var pi=Ae;export{Ir as createEffectsHandler,pi as default};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "route-engine-js",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/RouteEngine.js",
|
|
6
6
|
"exports": {
|
|
@@ -14,9 +14,9 @@
|
|
|
14
14
|
"test": "vitest run",
|
|
15
15
|
"test:watch": "vitest",
|
|
16
16
|
"build": "bun run esbuild.js",
|
|
17
|
-
"lint": "
|
|
18
|
-
"lint:fix": "
|
|
19
|
-
"coverage": "vitest run --coverage",
|
|
17
|
+
"lint": "bun run prettier src -c",
|
|
18
|
+
"lint:fix": "bun run prettier src -w",
|
|
19
|
+
"coverage": "bun run vitest run --coverage",
|
|
20
20
|
"vt:generate": "bun run esbuild.js && rtgl vt generate --skip-screenshots",
|
|
21
21
|
"serve": "bun run esbuild.js && bunx serve -p 3004 .rettangoli/vt/_site"
|
|
22
22
|
},
|
|
@@ -29,6 +29,8 @@
|
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@vitest/coverage-v8": "^3.2.4",
|
|
31
31
|
"@vitest/ui": "^3.2.4",
|
|
32
|
-
"
|
|
32
|
+
"husky": "^9.1.7",
|
|
33
|
+
"prettier": "^3.7.4",
|
|
34
|
+
"vitest": "^4.0.16"
|
|
33
35
|
}
|
|
34
36
|
}
|