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ACX,IAAA,IAAA,CAAK,OAAA,GAAU,MAAA;AACf,IAAA,OAAO,IAAA;AAAA,EACX;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAaA,MACI,KAAA,EACI;AACJ,IAAA,IAAA,CAAK,IAAA,CAAK,sBAAA,CAAuB,KAAA,EAAO,KAAK,CAAA;AAAA,EACjD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,OACI,KAAA,EACI;AACJ,IAAA,IAAA,CAAK,IAAA,CAAK,sBAAA,CAAuB,MAAA,EAAQ,KAAK,CAAA;AAAA,EAClD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,OACI,KAAA,EACI;AACJ,IAAA,IAAA,CAAK,IAAA,CAAK,sBAAA,CAAuB,MAAA,EAAQ,KAAK,CAAA;AAAA,EAClD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,MAAA,GAAY;AACR,IAAA,OAAO;AAAA,MACH,KAAA,EAAO,IAAA,CAAK,KAAA,CAAM,QAAA,EAAS;AAAA,MAC3B,MAAM,IAAA,CAAK,KAAA;AAAA,MACX,MAAM,IAAA,CAAK,IAAA;AAAA,MACX,OAAO,IAAA,CAAK,KAAA;AAAA,MACZ,QAAQ,IAAA,CAAK,MAAA;AAAA,MACb,SAAS,IAAA,CAAK;AAAA,KAClB;AAAA,EACJ;AAEJ;AA5Oa,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":"AreInstruction.entity.mjs","sourcesContent":["\nimport { A_CommonHelper, A_Context, A_Entity, A_Error, A_FormatterHelper, A_Scope, ASEID } from \"@adaas/a-concept\";\nimport { A_Frame } from \"@adaas/a-frame/core\";\nimport type { AreNode } from \"@adaas/are/node/AreNode.entity\";\nimport { AreInstructionNewProps, AreInstructionSerialized } from \"./AreInstruction.types\";\nimport { AreInstructionFeatures } from \"./AreInstruction.constants\";\nimport { AreStoreWatchingEntity } from \"@adaas/are/store/AreStore.types\";\n\n\n@A_Frame.Define({\n    namespace: 'A-ARE',\n    description: 'AreInstruction is the base entity for all rendering instructions in the ARE framework. It represents a serializable, reversible operation (such as creating or mutating a DOM element) that can be applied to and tracked within the AreScene, enabling deterministic rendering and undo/redo capabilities.'\n})\nexport class AreInstruction<\n    T extends Record<string, any> = Record<string, any>,\n    S extends AreInstructionSerialized<T> = AreInstructionSerialized<T>\n> extends A_Entity<AreInstructionNewProps<T>, S> implements AreStoreWatchingEntity {\n\n    static get concept(): string {\n        return 'are';\n    }\n\n    /**\n     * The name of the instruction, for example \"CreateElement\", \"AddAttribute\", \"RemoveNode\", etc. This is used to identify the type of the instruction and how to process it. The name should be in PascalCase format, and should be unique across all instruction types. It is recommended to use a prefix that indicates the category of the instruction, for example \"CreateElement\" for instructions that create new elements, \"UpdateAttribute\" for instructions that update attributes, etc.\n     */\n    protected _name!: string\n    /**\n     * The payload of the instruction, which can contain any additional information that may be needed for the rendering purpose. For example, for CreateElement instruction, the payload can contain the tag name and parent information, so the Host can use this information to create the element in the correct place in the scene. The payload is optional and can be an empty object if no additional information is needed.\n     */\n    protected _payload?: T\n    /**\n     * Group is an optional property that can be used to group instructions together. For example a set of instructions that depend on create CreateElement instruction can be grouped together with the same group name, so if the CreateElement instruction is reverted, all the instructions in the same group will be reverted as well, and so on. This can be useful to manage complex changes that involve multiple instructions. \n     * \n     * [!] Note, the best option is to use ASEID of the Instruction as a group, so all instructions with the same ASEID will be treated as a single change, and will be applied and reverted together.\n     */\n    protected _group: string | undefined\n    /**\n     * The parent instruction that created this instruction. For example, if we have a CreateElement instruction that creates a new element, and then we have an AddAttribute instruction that adds an attribute to that element, the AddAttribute instruction would have the CreateElement instruction as its parent. This can be used to track the hierarchy of instructions and their dependencies.\n     */\n    protected _parent: string | undefined\n\n    /**\n     * A set of properties that influence the behavior of the instruction, for example, for AddTextInstruction, we can interpolation dependent on some key in the store, so we can have a property called \"interpolationKey\" that will be used to track the dependencies of the instruction, and when the value of this key changes in the scope, we can update the instruction accordingly.\n     */\n    protected _props!: Set<string>\n\n\n    /**\n     * The name of the instruction, for example \"CreateElement\", \"AddAttribute\", \"RemoveNode\", etc. This is used to identify the type of the instruction and how to process it. The name should be in PascalCase format, and should be unique across all instruction types. It is recommended to use a prefix that indicates the category of the instruction, for example \"CreateElement\" for instructions that create new elements, \"UpdateAttribute\" for instructions that update attributes, etc.\n     */\n    get name(): string {\n        return this._name;\n    }\n    /**\n     * The payload of the instruction, which can contain any additional information that may be needed for the rendering purpose. For example, for CreateElement instruction, the payload can contain the tag name and parent information, so the Host can use this information to create the element in the correct place in the scene. The payload is optional and can be an empty object if no additional information is needed. \n     * \n     * [!] Note, the payload should be serializable, so it can be stored and transmitted easily. It is recommended to use simple data structures for the payload, such as objects, arrays, strings, numbers, etc., and avoid using complex data types that may not be easily serializable.\n     */\n    get payload(): T {\n        return this._payload || {} as T;\n    }\n\n    /**\n     * Group is an optional property that can be used to group instructions together. For example a set of instructions that depend on create CreateElement instruction can be grouped together with the same group name, so if the CreateElement instruction is reverted, all the instructions in the same group will be reverted as well, and so on. This can be useful to manage complex changes that involve multiple instructions. \n     * \n     * [!] Note, the best option is to use ASEID of the Instruction as a group, so all instructions with the same ASEID will be treated as a single change, and will be applied and reverted together.\n     */\n    get group(): string | undefined {\n        return this._group;\n    }\n    /**\n     * The parent instruction ASEID that created this instruction. For example, if we have a CreateElement instruction that creates a new element, and then we have an AddAttribute instruction that adds an attribute to that element, the AddAttribute instruction would have the CreateElement instruction as its parent. This can be used to track the hierarchy of instructions and their dependencies.\n     * \n     * [!] Note, the parent should be provided as an ASEID string, so it can be easily referenced and tracked across different contexts and times.\n     */\n    get parent(): string | undefined {\n        return this._parent;\n    }\n\n    get id(): string {\n        return this.aseid.id;\n    }\n\n    /**\n     * A stable discriminator that identifies the concrete instruction class (e.g. \"AreDeclaration\", \"AreMutation\"). It is used during serialization so a prebuilt/serialized scene can be mapped back to the correct instruction class via `scope.resolveConstructor(type)` during deserialization.\n     *\n     * [!] Note, this uses the canonical component name (`A_CommonHelper.getComponentName`) — the same naming the DI resolution (`resolveConstructor`) consumes — so a serialized instruction is resolvable without a separate registry.\n     */\n    get type(): string {\n        return A_CommonHelper.getComponentName(this.constructor);\n    }\n\n    get owner(): AreNode {\n        return A_Context.scope(this).issuer<AreNode>()!;\n    }\n\n\n    fromNew(newEntity: AreInstructionNewProps<T>): void {\n\n        // TODO: Verify if deduplication ID is needed\n        // const identity = newEntity.id || {\n        //     name: newEntity.name,\n        // };\n\n        // const id = AreCacheHelper.createHash(identity);\n\n        this.aseid = this.generateASEID({\n            // shard: newEntity.node.id,\n            entity: A_FormatterHelper.toKebabCase(newEntity.name),\n            // id: id,\n        });\n\n        this._name = newEntity.name;\n        this._payload = newEntity.payload\n        this._group = newEntity.group?.aseid.toString();\n        this._parent = newEntity.parent?.aseid.toString();\n    }\n\n\n    /**\n     * Reconstructs the instruction from its serialized (runtime-free) form, restoring its identity and structural state.\n     *\n     * Restored: `aseid`, `name`, `payload`, `group` and `parent` (the parent/group references are already stored as ASEID strings, so no remapping is required for a restore-mode rehydration).\n     * Not restored: runtime-only state (store-dependency tracking, applied/reverted state) — it is re-derived when the instruction is interpreted again.\n     *\n     * @param serialized the serialized representation produced by `toJSON()`.\n     */\n    fromJSON(serialized: S): void {\n        this.aseid = new ASEID(serialized.aseid);\n\n        this._name = serialized.name;\n        this._payload = serialized.payload;\n        this._group = serialized.group;\n        this._parent = serialized.parent;\n    }\n\n\n    fromUndefined(): void {\n        throw new A_Error({\n            title: \"Cannot create an instruction without properties\",\n            description: \"AreInstruction cannot be created without properties. Please provide the necessary properties to create an instruction.\",\n        })\n    }\n\n\n    // ===============================================================================\n    // ----------------------------Instruction Operations ------------------------------\n    // ===============================================================================\n\n    /**\n     * Group this instruction with another instruction. This means that when one of the instructions in the group is applied or reverted, all the instructions in the same group will be applied or reverted together. This can be useful to manage complex changes that involve multiple instructions. \n     * \n     * For example, if we have a CreateElement instruction that creates a new element, and then we have an AddAttribute instruction that adds an attribute to that element, we can group them together with the same group name, so if we revert the CreateElement instruction, the AddAttribute instruction will be reverted as well, and so on.\n     * \n     * @param instruction \n     * @returns \n     */\n    groupWith(instruction: AreInstruction): this {\n        this._group = instruction.id;\n        return this;\n    }\n    /**\n     * Ungroup this instruction from any group. This means that this instruction will be treated as an independent instruction, and will not be applied or reverted together with any other instructions. This can be useful when you want to separate an instruction from a group, so it can be applied or reverted independently.\n     * \n     * @returns \n     */\n    unGroup(): this {\n        this._group = undefined;\n        return this;\n    }\n    /**\n     * Attach this instruction to a parent instruction. This means that this instruction will be considered as a child of the parent instruction, and can be used to track the hierarchy of instructions and their dependencies. \n     * \n     * For example, if we have a CreateElement instruction that creates a new element, and then we have an AddAttribute instruction that adds an attribute to that element, we can attach the AddAttribute instruction to the CreateElement instruction as its parent, so we can track that the AddAttribute instruction is related to the CreateElement instruction.\n     * \n     * @param parent \n     * @returns \n     */\n    attachTo(parent: AreInstruction): this {\n        this._parent = parent.id;\n        return this;\n    }\n    /**\n     * Detach this instruction from its parent instruction. This means that this instruction will no longer be considered as a child of the parent instruction, and will not be related to it in any way. This can be useful when you want to separate an instruction from its parent, so it can be treated as an independent instruction.\n     * \n     * @returns \n     */\n    detach(): this {\n        this._parent = undefined;\n        return this;\n    }\n\n\n    // ===============================================================================\n    // ----------------------------Instruction Features ------------------------------\n    // ===============================================================================\n    /**\n     * Apply this instruction to the scene. This means that the changes represented by this instruction will be applied to the scene, and the Host will perform the necessary operations to reflect these changes in the rendered output. \n     * \n     * For example, if this instruction is a CreateElement instruction, when we apply it, the Host will create a new element in the scene according to the information provided in the payload of the instruction. If this instruction is an AddAttribute instruction, when we apply it, the Host will add the specified attribute to the target element in the scene. The apply method can also accept an optional scope parameter, which can be used to provide additional context or information that may be needed for applying the instruction.\n     * \n     * @param scope \n     */\n    apply(\n        scope?: A_Scope\n    ): void {\n        this.call(AreInstructionFeatures.Apply, scope);\n    }\n    /**\n     * Update this instruction in the scene. This means that the changes represented by this instruction will be updated in the scene, and the Host will perform the necessary operations to reflect these changes in the rendered output. This is particularly useful for instructions that have dynamic properties or effects that may change over time, allowing for adjustments to be made to the instruction's behavior or effects without needing to revert and reapply it entirely. The update method can also accept an optional scope parameter, which can be used to provide additional context or information that may be needed for updating the instruction.\n     * \n     * @param scope \n     */\n    update(\n        scope?: A_Scope\n    ): void {\n        this.call(AreInstructionFeatures.Update, scope);\n    }\n    /**\n     * Revert this instruction from the scene. This means that the changes represented by this instruction will be reverted from the scene, and the Host will perform the necessary operations to undo these changes in the rendered output.\n     * \n     * @param scope \n     */\n    revert(\n        scope?: A_Scope\n    ): void {\n        this.call(AreInstructionFeatures.Revert, scope);\n    }\n\n\n    /**\n     * Serializes the instruction into its structural form, dropping all runtime-only state.\n     *\n     * Kept (static / structural): `aseid`, `name`, `type` discriminator, `group`, `parent` and `payload`.\n     * Dropped (runtime-only): `_props` (the live store-dependency tracking set) and any applied/reverted state, which must be re-derived when the instruction is interpreted again.\n     *\n     * @returns the serialized, runtime-free representation of the instruction.\n     */\n    toJSON(): S {\n        return {\n            aseid: this.aseid.toString(),\n            name: this._name,\n            type: this.type,\n            group: this.group,\n            parent: this.parent,\n            payload: this.payload,\n        } as S;\n    }\n\n}\n\n\n\n\n\n\n\n\n"]}