{"version":3,"file":"plugin_handle.cjs","names":[],"sources":["../src/plugin_handle.ts"],"sourcesContent":["/**\n * PluginHandle — Opaque branded handle and output key utilities for plugin instances.\n *\n * Extracted into its own module to break circular dependencies:\n * both block_storage.ts and plugin_model.ts can import from here\n * without depending on each other.\n *\n * @module plugin_handle\n */\n\nimport type { Branded } from \"@milaboratories/helpers\";\n\n/**\n * Phantom-only base type for constraining PluginHandle's type parameter.\n *\n * PluginFactory has create() → PluginInstance with function properties, making it invariant\n * under strictFunctionTypes. PluginFactoryLike exposes only the covariant `__types` phantom,\n * avoiding the contravariance chain. Handles only need `__types` for type extraction.\n *\n * PluginFactory extends PluginFactoryLike, so every concrete factory satisfies this constraint.\n */\nexport interface PluginFactoryLike<\n  Data extends Record<string, unknown> = Record<string, unknown>,\n  Params extends undefined | Record<string, unknown> = undefined | Record<string, unknown>,\n  Outputs extends Record<string, unknown> = Record<string, unknown>,\n  ModelServices = unknown,\n  UiServices = unknown,\n> {\n  readonly __types?: {\n    data: Data;\n    params: Params;\n    outputs: Outputs;\n    modelServices: ModelServices;\n    uiServices: UiServices;\n  };\n}\n\n/** Extract the Data type from a PluginFactoryLike phantom. */\nexport type InferFactoryData<F extends PluginFactoryLike> = NonNullable<\n  F extends PluginFactoryLike<infer D, any, any, any, any> ? D : Record<string, unknown>\n>;\n\n/** Extract the Params type from a PluginFactoryLike phantom. */\nexport type InferFactoryParams<F extends PluginFactoryLike> = NonNullable<\n  F extends PluginFactoryLike<any, infer P, any, any, any> ? P : undefined\n>;\n\n/** Extract the Outputs type from a PluginFactoryLike phantom. */\nexport type InferFactoryOutputs<F extends PluginFactoryLike> = NonNullable<\n  F extends PluginFactoryLike<any, any, infer O, any, any> ? O : Record<string, unknown>\n>;\n\n/** Extract the pre-resolved model services type from a PluginFactoryLike phantom. */\nexport type InferFactoryModelServices<F extends PluginFactoryLike> =\n  F extends PluginFactoryLike<any, any, any, infer ModelServices, any> ? ModelServices : {};\n\n/** Extract the pre-resolved UI services type from a PluginFactoryLike phantom. */\nexport type InferFactoryUiServices<F extends PluginFactoryLike> =\n  F extends PluginFactoryLike<any, any, any, any, infer UiServices> ? UiServices : {};\n\n/**\n * Derive a typed PluginHandle from a PluginFactory type.\n * Normalizes the brand to data/params/outputs/services (strips config) so handles\n * from InferPluginUiEntries match handles from InferPluginHandle.\n */\nexport type InferPluginHandle<F extends PluginFactoryLike> = NonNullable<\n  F extends PluginFactoryLike<\n    infer Data,\n    infer Params,\n    infer Outputs,\n    infer ModelServices,\n    infer UiServices\n  >\n    ? PluginHandle<PluginFactoryLike<Data, Params, Outputs, ModelServices, UiServices>>\n    : PluginHandle\n>;\n\n/**\n * Opaque handle for a plugin instance. Runtime value is the plugin instance ID string.\n * Branded with factory phantom `F` for type-safe data/outputs extraction.\n * Constrained with PluginFactoryLike (not PluginFactory) to avoid variance issues.\n */\nexport type PluginHandle<F extends PluginFactoryLike = PluginFactoryLike> = Branded<string, F>;\n\nconst PLUGIN_OUTPUT_PREFIX = \"plugin-output#\";\n\n/** Construct the output key for a plugin output in the block outputs map. */\nexport function pluginOutputKey<F extends PluginFactoryLike>(\n  handle: PluginHandle<F>,\n  outputKey: string,\n): string {\n  return `${PLUGIN_OUTPUT_PREFIX}${handle}#${outputKey}`;\n}\n\n/** Check whether an output key belongs to a plugin (vs block-level output). */\nexport function isPluginOutputKey(key: string): boolean {\n  return key.startsWith(PLUGIN_OUTPUT_PREFIX);\n}\n\n/** Get the prefix used for all outputs of a specific plugin instance. */\nexport function pluginOutputPrefix<F extends PluginFactoryLike>(handle: PluginHandle<F>): string {\n  return pluginOutputKey(handle, \"\");\n}\n"],"mappings":";AAoFA,MAAM,uBAAuB;;AAG7B,SAAgB,gBACd,QACA,WACQ;CACR,OAAO,GAAG,uBAAuB,OAAO,GAAG;AAC7C;;AAGA,SAAgB,kBAAkB,KAAsB;CACtD,OAAO,IAAI,WAAW,oBAAoB;AAC5C;;AAGA,SAAgB,mBAAgD,QAAiC;CAC/F,OAAO,gBAAgB,QAAQ,EAAE;AACnC"}