# DO NOT EDIT
# This file is automatically generated by autobean_refactor.modelgen.

from typing import Type, TypeVar, final
from .. import base, internal
from ..cost import TotalCost, UnitCost

_Self = TypeVar('_Self', bound='CostSpec')


@internal.tree_model
class CostSpec(base.RawTreeModel, internal.SpacingAccessorsMixin):
    RULE = 'cost_spec'

    _cost = internal.required_field[TotalCost | UnitCost]()

    raw_cost = internal.required_node_property[TotalCost | UnitCost](_cost)

    @final
    def __init__(
            self,
            token_store: base.TokenStore,
            cost: TotalCost | UnitCost,
    ):
        super().__init__(token_store)
        self._cost = cost

    @property
    def first_token(self) -> base.RawTokenModel:
        return self._cost.first_token

    @property
    def last_token(self) -> base.RawTokenModel:
        return self._cost.last_token

    def clone(self: _Self, token_store: base.TokenStore, token_transformer: base.TokenTransformer) -> _Self:
        return type(self)(
            token_store,
            type(self)._cost.clone(self._cost, token_store, token_transformer),
        )

    def _reattach(self, token_store: base.TokenStore, token_transformer: base.TokenTransformer) -> None:
        self._token_store = token_store
        self._cost = type(self)._cost.reattach(self._cost, token_store, token_transformer)

    def _eq(self, other: base.RawTreeModel) -> bool:
        return (
            isinstance(other, CostSpec)
            and self._cost == other._cost
        )

    @classmethod
    def from_children(
            cls: Type[_Self],
            cost: TotalCost | UnitCost,
    ) -> _Self:
        tokens = [
            *cost.detach(),
        ]
        token_store = base.TokenStore.from_tokens(tokens)
        cls._cost.reattach(cost, token_store)
        return cls(token_store, cost)

    def auto_claim_comments(self) -> None:
        type(self)._cost.auto_claim_comments(self._cost)
