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,CAAA;AAAA,CAAA,EAhHd,cAAA,CA2GC,SAAA,EAAA,kBAAA,EAAA,CAAA,CAAA;AAmCA,eAAA,CAAA;AAAA,EAJT,UAAU,MAAA,CAAO;AAAA,IACd,MAAM,sBAAA,CAAuB,MAAA;AAAA,IAC7B,KAAA,EAAO,CAAC,cAAc;AAAA,GACzB,CAAA;AAAA,EAEI,4BAAS,QAAQ,CAAA,CAAA;AAAA,EACjB,4BAAS,cAAc,CAAA,CAAA;AAAA,EACvB,4BAAS,QAAQ,CAAA,CAAA;AAAA,EACjB,4BAAS,OAAO,CAAA,CAAA;AAAA,EAChB,4BAAS,SAAS,CAAA;AAAA,CAAA,EAnJd,cAAA,CA8IC,SAAA,EAAA,mBAAA,EAAA,CAAA,CAAA;AAmCA,eAAA,CAAA;AAAA,EAJT,UAAU,MAAA,CAAO;AAAA,IACd,MAAM,sBAAA,CAAuB,MAAA;AAAA,IAC7B,KAAA,EAAO,CAAC,cAAc;AAAA,GACzB,CAAA;AAAA,EAEI,4BAAS,QAAQ,CAAA,CAAA;AAAA,EACjB,4BAAS,cAAc,CAAA,CAAA;AAAA,EACvB,4BAAS,QAAQ,CAAA,CAAA;AAAA,EACjB,4BAAS,OAAO,CAAA,CAAA;AAAA,EAChB,4BAAS,SAAS,CAAA;AAAA,CAAA,EAtLd,cAAA,CAiLC,SAAA,EAAA,mBAAA,EAAA,CAAA,CAAA;AAjLD,cAAA,GAAN,eAAA,CAAA;AAAA,EAJN,QAAQ,MAAA,CAAO;AAAA,IACZ,SAAA,EAAW,OAAA;AAAA,IACX,WAAA,EAAa;AAAA,GAChB;AAAA,CAAA,EACY,cAAA,CAAA","file":"AreInterpreter.component.mjs","sourcesContent":["import { A_Caller, A_Component, A_Feature, A_Inject, A_Scope, A_TYPES__Entity_Constructor } from \"@adaas/a-concept\";\nimport { A_Frame } from \"@adaas/a-frame/core\";\nimport { AreScene } from \"@adaas/are/scene/AreScene.context\";\nimport { AreNode } from \"@adaas/are/node/AreNode.entity\";\nimport { AreNodeFeatures } from \"@adaas/are/node/AreNode.constants\";\nimport { AreInstructionFeatures } from \"@adaas/are/instruction/AreInstruction.constants\";\nimport { AreInstruction } from \"@adaas/are/instruction/AreInstruction.entity\";\nimport { AreStore } from \"@adaas/are/store/AreStore.context\";\nimport { A_Logger } from \"@adaas/a-utils/a-logger\";\n\n\n\n@A_Frame.Define({\n    namespace: 'A-ARE',\n    description: 'Stateless executor that reads the Scene and translates its instructions into operations on a rendering target. Computes the diff between applied and planned, calls revert on removed instructions and apply on added ones. Owns no state of its own — all state lives in the Scene. Can be swapped for any target implementation (DOMInterpreter, SSRInterpreter, CanvasInterpreter) without touching any other part of the pipeline.'\n})\nexport class AreInterpreter extends A_Component {\n    /**\n     * Decorator to mark a method as an instruction Apply handler for the specific instruction type. The method will be called during the render phase of the ARE component when the corresponding instruction needs to be applied. The method should contain logic to perform the necessary operations on the rendering target based on the instruction's content and context.\n     * \n     * @param action \n     * @returns \n     */\n    static Apply(action: string) {\n        const name = action + AreInstructionFeatures.Apply\n\n        return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {\n            return A_Feature.Extend({\n                name,\n                scope: [target.constructor],\n            })(target, propertyKey, descriptor);\n        }\n    }\n    /**\n     * Decorator to mark a method as an instruction Update handler for the specific instruction type. The method will be called during the render phase of the ARE component when the corresponding instruction has been updated. The method should contain logic to perform the necessary operations on the rendering target to update the effects of the instruction based on its new content and context.\n     * \n     * @param action \n     * @returns \n     */\n    static Update(action: string) {\n        const name = action + AreInstructionFeatures.Update\n\n        return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {\n            return A_Feature.Extend({\n                name,\n                scope: [target.constructor],\n            })(target, propertyKey, descriptor);\n        }\n    }\n    /**\n     * Decorator to mark a method as an instruction Revert handler for the specific instruction type. The method will be called during the render phase of the ARE component when the corresponding instruction needs to be reverted. The method should contain logic to perform the necessary operations on the rendering target to undo the effects of the instruction based on its content and context.\n     * \n     * @param action \n     * @returns \n     */\n    static Revert(action: string) {\n\n        const name = action + AreInstructionFeatures.Revert\n\n        return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {\n            return A_Feature.Extend({\n                name,\n                scope: [target.constructor],\n            })(target, propertyKey, descriptor);\n        }\n    }\n    /**\n     * The method responsible for executing the render operation based on the current state of the Scene. It processes the instructions that need to be applied and reverted, ensuring that the rendering target is updated accordingly. The method handles any errors that may occur during the application or reversion of instructions, maintaining the integrity of the rendering process.\n     * \n     * @param scene \n     */\n    @A_Feature.Extend({\n        name: AreNodeFeatures.onInterpret,\n        scope: [AreNode]\n    })\n    interpret(\n        @A_Inject(AreScene) scene: AreScene,\n    ) {\n        /**\n          * 1. First we need to get all changes to be applied and reverted during render operation\n          */\n        const { toApply, toRevert } = scene.changes\n\n        /**\n         * 2. Then we need to revert all instructions from scene\n         */\n        for (const instruction of toRevert) {\n            try {\n                instruction.revert();\n                scene.unApply(instruction);\n            } catch (error) {\n                instruction.apply();\n                scene.apply(instruction);\n            }\n        }\n        /**\n         * 3. Finally we should apply everything that needs to be applied\n         */\n        for (const instruction of toApply) {\n            try {\n                /**\n                 * 3.1. if everything went well then just simply apply and attach to state this instruction\n                 */\n                instruction.apply();\n                scene.apply(instruction);\n            } catch (error) {\n                /**\n                 * 2.2. if any error happened we simply revert the instruction and remove it from the state\n                 */\n                instruction.revert();\n                scene.unApply(instruction);\n            }\n        }\n    }\n\n    // -----------------------------------------------------------------------------------------\n    // ----------------------------Are-Instruction Section----------------------------------------\n    // -----------------------------------------------------------------------------------------\n\n    @A_Feature.Extend({\n        name: AreInstructionFeatures.Apply,\n        scope: [AreInstruction]\n    })\n    protected applyInstruction(\n        @A_Inject(A_Caller) instruction: AreInstruction,\n        @A_Inject(AreInterpreter) interpreter: AreInterpreter,\n        @A_Inject(AreStore) store: AreStore,\n        @A_Inject(A_Scope) scope: A_Scope,\n        @A_Inject(A_Feature) feature: A_Feature,\n        ...args: any[]\n    ) {\n        try {\n            /**\n             * 1. uses to track all calls to Store\n             *\n             * `store` is optional: it is only registered for nodes that have a\n             * component (see AreLifecycle.init). An instruction belonging to a\n             * node whose scope chain has no component ancestor (e.g. top-level\n             * text) has no store to read from, so there is nothing to track.\n             */\n            store?.watch(instruction);\n            /**\n             * So we're looking for any instruction name in the interpreter to be executed.\n             */\n            feature.chain(interpreter, instruction.name + AreInstructionFeatures.Apply, scope);\n\n            store?.unwatch(instruction);\n        } catch (error) {\n            store?.unwatch(instruction);\n            throw error;\n        }\n\n    }\n\n    @A_Feature.Extend({\n        name: AreInstructionFeatures.Update,\n        scope: [AreInstruction]\n    })\n    protected updateInstruction(\n        @A_Inject(A_Caller) instruction: AreInstruction,\n        @A_Inject(AreInterpreter) interpreter: AreInterpreter,\n        @A_Inject(AreStore) store: AreStore,\n        @A_Inject(A_Scope) scope: A_Scope,\n        @A_Inject(A_Feature) feature: A_Feature,\n        ...args: any[]\n    ) {\n        try {\n            /**\n             * 1. uses to track all calls to Store\n             *\n             * `reevaluate = true`: an Update is a fresh re-evaluation of the\n             * instruction, so its previously recorded store dependencies are\n             * pruned first and rebuilt from this pass's reads (#5).\n             */\n            store?.watch(instruction, true);\n            /**\n             * So we're looking for any instruction name in the interpreter to be executed.\n             */\n            feature.chain(interpreter, instruction.name + AreInstructionFeatures.Update, scope);\n\n            store?.unwatch(instruction);\n        } catch (error) {\n            store?.unwatch(instruction);\n            throw error;\n        }\n\n    }\n\n\n    @A_Feature.Extend({\n        name: AreInstructionFeatures.Revert,\n        scope: [AreInstruction]\n    })\n    protected revertInstruction(\n        @A_Inject(A_Caller) instruction: AreInstruction,\n        @A_Inject(AreInterpreter) interpreter: AreInterpreter,\n        @A_Inject(AreStore) store: AreStore,\n        @A_Inject(A_Scope) scope: A_Scope,\n        @A_Inject(A_Feature) feature: A_Feature,\n        ...args: any[]\n    ) {\n        try {\n            /**\n             * So we're looking for any instruction name in the interpreter to be executed.\n             */\n            feature.chain(interpreter, instruction.name + AreInstructionFeatures.Revert, scope);\n\n            /**\n             * The instruction is being torn down — drop every store\n             * subscription it holds so a later set() can never re-notify a\n             * reverted/unmounted watcher (prevents the dependency-map leak and\n             * zombie updates).\n             *\n             * `store` is optional (only present for component-backed nodes), so\n             * guard the teardown — a node outside any store holds no\n             * subscriptions to drop. Previously an unconditional call here threw\n             * on the undefined store and masked the real apply-time error.\n             */\n            store?.unregister(instruction);\n        } catch (error) {\n            store?.unregister(instruction);\n            throw error;\n        }\n    }\n}"]}