import { ModelContract } from "src/ts/types/core/base/model.type"; import { ViewContract } from "src/ts/types/core/base/view.type"; import { Lifecycle } from "./lifecycle"; import { LifecycleState } from "src/ts/types/core/base/lifecycle.type"; /** * Base model primitive that binds a domain object to a target DOM element and an optional View. * * This class is the **Model** part of the library's Model/View separation: * - The **Model** owns references to the authoritative DOM source (`targetElement`) and configuration (`options`). * - The **View** (if attached) owns rendering and DOM event wiring for the model. * - Higher-level infrastructure (e.g., Adapter / RecyclerView) orchestrates when models are created, * bound to views, and updated. * * ### Lifecycle (Strict FSM) * - Constructor calls {@link Lifecycle.init} immediately, transitioning `NEW → INITIALIZED`. * - This base model does not call `mount()` by itself; mounting is typically handled by the View layer. * - {@link updateTarget} triggers {@link Lifecycle.update}, which emits `onUpdate` lifecycle hooks in * `MOUNTED/UPDATED` states (and is guarded otherwise). * - {@link destroy} transitions to `DESTROYED`, clears references, and destroys the associated view. * * ### Idempotency / No-ops * - {@link destroy} is idempotent once in {@link LifecycleState.DESTROYED}. * - {@link updateTarget} is safe to call multiple times; consumers should treat repeated assignments * as a no-op when the target does not change (this base class does not compare equality). * * ### Ownership & side effects * - This model **owns** its `view` reference and will call `view.destroy()` during {@link destroy}. * - The model itself does not mutate the DOM, except reading from `targetElement` (e.g., {@link value}). * Any DOM side effects are expected to live in the View implementation. * * @template TTarget - The DOM element type this model is bound to (e.g., HTMLOptionElement). * @template TTags - Named element map used by the view (view-specific DOM handles). * @template TView - View implementation associated with this model. * @template TOptions - Configuration/options type carried by the model. * * @implements {ModelContract} * @extends Lifecycle * @see {@link ViewContract} * @see {@link LifecycleState} */ export class Model< TTarget extends HTMLElement, TTags extends Record, TView extends ViewContract, TOptions = unknown, > extends Lifecycle implements ModelContract { /** * The currently bound target DOM element. * * This element typically represents the source-of-truth node in the host DOM (e.g., a native `