# Reusable hard preservation and clearance components.
from dataclasses import dataclass
from hearth.kernel import Capability, Contract, Plan, Rule
from hearth.space import Box


@dataclass(frozen=True)
class Preserve:
    region: Box

    def capability(self):
        return Capability('constraint.preserve', ('constraint', 'protected-region'), guarantees=('immutable existing region',))

    def negotiate(self, ctx, parameters):
        cells = tuple(self.region.cells())
        return Contract(self.region, rules=(Rule('protected', cells, self.region, {'states': [ctx.view.state(ctx.world(p)) for p in cells]}),))

    def realize(self, ctx, contract):
        return Plan()
