var{defineProperty:G,getOwnPropertyNames:I,getOwnPropertyDescriptor:J}=Object,L=Object.prototype.hasOwnProperty;function N(g){return this[g]}var U=(g)=>{var j=(H??=new WeakMap).get(g),x;if(j)return j;if(j=G({},"__esModule",{value:!0}),g&&typeof g==="object"||typeof g==="function"){for(var z of I(g))if(!L.call(j,z))G(j,z,{get:N.bind(g,z),enumerable:!(x=J(g,z))||x.enumerable})}return H.set(g,j),j},H;var O=(g)=>g;function P(g,j){this[g]=O.bind(null,j)}var W=(g,j)=>{for(var x in j)G(g,x,{get:j[x],enumerable:!0,configurable:!0,set:P.bind(j,x)})};var X=(g,j)=>()=>(g&&(j=g(g=0)),j);import{compareVersions as Q}from"compare-versions";var F=(g,j)=>`${g}.v${j}`;class S{items=new Map;surfaceIndex=null;register(g){let j=F(g.meta.key,g.meta.version);if(this.items.has(j))throw Error(`Duplicate capability ${j}`);return this.items.set(j,g),this.surfaceIndex=null,this}list(){return[...this.items.values()]}get(g,j){if(j!=null)return this.items.get(F(g,j));let x;for(let z of this.items.values()){if(z.meta.key!==g)continue;if(!x||Q(z.meta.version,x.meta.version)>0)x=z}return x}satisfies(g,j){if(g.optional)return!0;if(j?.some((z)=>T(z,g)))return!0;let x=g.version?this.get(g.key,g.version):this.get(g.key);if(!x)return!1;if(g.kind&&x.meta.kind!==g.kind)return!1;if(g.version!=null&&x.meta.version!==g.version)return!1;return!0}buildSurfaceIndex(){if(this.surfaceIndex)return this.surfaceIndex;this.surfaceIndex=new Map;for(let g of this.items.values()){let j=F(g.meta.key,g.meta.version);for(let x of g.provides??[]){let z=`${x.surface}:${x.key}`;if(!this.surfaceIndex.has(z))this.surfaceIndex.set(z,new Set);this.surfaceIndex.get(z)?.add(j)}}return this.surfaceIndex}getOperationsFor(g,j){let x=this.get(g,j);if(!x)return[];return x.provides?.filter((z)=>z.surface==="operation").map((z)=>z.key)??[]}getEventsFor(g,j){let x=this.get(g,j);if(!x)return[];return x.provides?.filter((z)=>z.surface==="event").map((z)=>z.key)??[]}getPresentationsFor(g,j){let x=this.get(g,j);if(!x)return[];return x.provides?.filter((z)=>z.surface==="presentation").map((z)=>z.key)??[]}getWorkflowsFor(g,j){let x=this.get(g,j);if(!x)return[];return x.provides?.filter((z)=>z.surface==="workflow").map((z)=>z.key)??[]}getResourcesFor(g,j){let x=this.get(g,j);if(!x)return[];return x.provides?.filter((z)=>z.surface==="resource").map((z)=>z.key)??[]}getCapabilitiesForOperation(g){let x=this.buildSurfaceIndex().get(`operation:${g}`);if(!x)return[];return[...x].map((z)=>{let B=this.items.get(z);return{key:B?.meta.key??"",version:B?.meta.version??""}})}getCapabilitiesForEvent(g){let x=this.buildSurfaceIndex().get(`event:${g}`);if(!x)return[];return[...x].map((z)=>{let B=this.items.get(z);return{key:B?.meta.key??"",version:B?.meta.version??""}})}getCapabilitiesForPresentation(g){let x=this.buildSurfaceIndex().get(`presentation:${g}`);if(!x)return[];return[...x].map((z)=>{let B=this.items.get(z);return{key:B?.meta.key??"",version:B?.meta.version??""}})}getAncestors(g,j){let x=[],z=new Set,B=this.get(g,j);while(B?.extends){let D=F(B.extends.key,B.extends.version);if(z.has(D))break;z.add(D);let E=this.get(B.extends.key,B.extends.version);if(!E)break;x.push(E),B=E}return x}getEffectiveRequirements(g,j){let x=this.get(g,j);if(!x)return[];let z=this.getAncestors(g,j),B=new Map;for(let D of[...z].reverse())for(let E of D.requires??[])B.set(E.key,E);for(let D of x.requires??[])B.set(D.key,D);return[...B.values()]}getEffectiveSurfaces(g,j){let x=this.get(g,j);if(!x)return[];let z=this.getAncestors(g,j),B=[];for(let D of[...z].reverse())B.push(...D.provides??[]);return B.push(...x.provides??[]),B}}function T(g,j){if(g.key!==j.key)return!1;if(j.version!=null&&g.version!==j.version)return!1;return!0}function _(g){return F(g.meta.key,g.meta.version)}function $(g){return g}var h=[{id:"docs.tech.contracts.capabilities",title:"CapabilitySpec Overview",summary:"Capability specs define what a module provides (operations, events, presentations) and requires (dependencies). They enable bidirectional linking, inheritance, runtime enforcement, and automated validation.",kind:"reference",visibility:"public",route:"/docs/tech/contracts/capabilities",tags:["tech","contracts","capabilities"],body:`# CapabilitySpec Overview

## Purpose

Capabilities are **module interfaces** that define:
1. What operations, events, and presentations a module exposes (\`provides\`)
2. What other capabilities it depends on (\`requires\`)
3. Inheritance hierarchies via \`extends\`

They enable:
- **Bidirectional linking**: Specs reference capabilities, capabilities list their specs
- **Dependency validation**: Features can't install without satisfying requirements
- **Runtime enforcement**: Check capabilities before executing operations
- **Inheritance**: Build capability hierarchies with shared requirements

## Schema

\`\`\`ts
export interface CapabilitySpec {
  meta: CapabilityMeta;              // ownership metadata + { key, version, kind }
  extends?: CapabilityRef;           // NEW: inherit from parent capability
  provides?: CapabilitySurfaceRef[]; // surfaces this capability exposes
  requires?: CapabilityRequirement[];// capabilities that must exist
}
\`\`\`

### Bidirectional Linking

Operations, events, and presentations can now declare their capability:

\`\`\`ts
// In OperationSpec
{
  meta: { key: 'payments.charge.create', ... },
  capability: { key: 'payments', version: '1.0.0' }, // Links to capability
  io: { ... }
}

// In CapabilitySpec
{
  meta: { key: 'payments', version: '1.0.0', ... },
  provides: [
    { surface: 'operation', key: 'payments.charge.create' }
  ]
}
\`\`\`

Validation ensures both sides match via \`validateCapabilityConsistency()\`.

## Registry Query Methods

The \`CapabilityRegistry\` now provides rich query capabilities:

\`\`\`ts
// Forward lookups: Capability → Specs
registry.getOperationsFor('payments');      // ['payments.charge.create', ...]
registry.getEventsFor('payments');          // ['payments.charge.succeeded', ...]
registry.getPresentationsFor('payments');   // ['payments.dashboard', ...]

// Reverse lookups: Spec → Capabilities
registry.getCapabilitiesForOperation('payments.charge.create');
registry.getCapabilitiesForEvent('payments.charge.succeeded');
registry.getCapabilitiesForPresentation('payments.dashboard');

// Inheritance
registry.getAncestors('payments.stripe');           // Parent chain
registry.getEffectiveRequirements('payments.stripe'); // Includes inherited
registry.getEffectiveSurfaces('payments.stripe');     // Includes inherited
\`\`\`

## Inheritance

Capabilities can extend other capabilities:

\`\`\`ts
// Base capability
defineCapability({
  meta: { key: 'payments.base', version: '1.0.0', ... },
  requires: [{ key: 'auth', version: '1.0.0' }],
  provides: [{ surface: 'operation', key: 'payments.list' }]
});

// Child capability inherits requirements
defineCapability({
  meta: { key: 'payments.stripe', version: '1.0.0', ... },
  extends: { key: 'payments.base', version: '1.0.0' },
  requires: [{ key: 'encryption', version: '1.0.0' }], // Added
  provides: [{ surface: 'operation', key: 'payments.stripe.charge' }]
});

// getEffectiveRequirements('payments.stripe') returns:
// [{ key: 'auth', ... }, { key: 'encryption', ... }]
\`\`\`

## Runtime Enforcement

Use \`CapabilityContext\` for opt-in runtime checks:

\`\`\`ts

// Create context from user's enabled capabilities
const ctx = createCapabilityContext(user.capabilities);

// Check capability
if (ctx.hasCapability('payments')) {
  // User can access payments features
}

// Assert capability (throws if missing)
ctx.requireCapability('payments');

// Guard an operation
assertCapabilityForOperation(ctx, paymentOperation);

// Filter operations by enabled capabilities
const allowedOps = filterOperationsByCapability(ctx, allOperations);
\`\`\`

## Validation

Validate bidirectional consistency between capabilities and specs:

\`\`\`ts

const result = validateCapabilityConsistency({
  capabilities: capabilityRegistry,
  operations: operationRegistry,
  events: eventRegistry,
  presentations: presentationRegistry,
});

if (!result.valid) {
  console.error('Validation errors:', result.errors);
}

// Find specs without capability assignment (informational)
const orphans = findOrphanSpecs({ capabilities, operations });
\`\`\`

## Feature Integration

During \`installFeature()\`:
1. \`provides\` capabilities must exist in the registry
2. \`requires\` must be satisfied by registered capabilities or local \`provides\`
3. Referenced operations/events/presentations must exist

## Authoring Guidelines

1. **Register capabilities first** before referencing them in features
2. **Use bidirectional linking** - set \`capability\` on specs and list them in \`provides\`
3. **Version consciously** - bump versions on breaking changes
4. **Document dependencies** with \`reason\` strings
5. **Use inheritance** for capability families with shared requirements
6. **Validate during CI** with \`validateCapabilityConsistency()\`

## Error Handling

\`\`\`ts

try {
  ctx.requireCapability('premium-features');
} catch (err) {
  if (err instanceof CapabilityMissingError) {
    console.log('Missing:', err.capabilityKey);
    console.log('Required version:', err.requiredVersion);
  }
}
\`\`\`

## API Reference

### Types
- \`CapabilitySpec\` - Capability definition
- \`CapabilityRef\` - Reference to a capability (key + version)
- \`CapabilitySurfaceRef\` - Reference to a provided surface
- \`CapabilityRequirement\` - Dependency requirement
- \`CapabilityContext\` - Runtime capability context
- \`CapabilityValidationResult\` - Validation result

### Functions
- \`defineCapability(spec)\` - Define a capability spec
- \`createCapabilityContext(caps)\` - Create runtime context
- \`validateCapabilityConsistency(deps)\` - Validate bidirectional links
- \`findOrphanSpecs(deps)\` - Find specs without capability assignment
- \`assertCapabilityForOperation/Event/Presentation(ctx, spec)\` - Guards
- \`filterOperationsByCapability(ctx, ops)\` - Filter by enabled capabilities
`}];export{h as tech_contracts_capabilities_DocBlocks,$ as defineCapability,_ as capabilityKey,S as CapabilityRegistry};
