"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateActionTypes = generateActionTypes;
const generator_1 = require("@postxl/generator");
function generateActionTypes({ providers, summary }) {
    const imports = generator_1.ImportGenerator.from(summary.action.location);
    if (providers.length === 0) {
        throw new Error('No action providers registered. At least one action provider is required!');
    }
    const actionType = [];
    const resultType = [];
    for (const { actions } of providers) {
        imports.addType(actions.summary.action);
        actionType.push(actions.summary.action.name);
        imports.addType(actions.summary.result);
        resultType.push(actions.summary.result.name);
    }
    if (providers.length === 0) {
        actionType.push(`{ scope:'NoOp', type:'NoOp', payload: undefined }`);
        resultType.push('any');
    }
    return /* ts */ `
${imports.generate()}

/**
 * A union of all action definitions.
 */
export type ${summary.action.name} = ${actionType.join(' | ')}

/**
 * A dictionary of all possible action results.
 */
export type ${summary.result.name} = ${resultType.join(' & ')}

/**
 * Extracts the result type of an action based on the inferred scope and type.
 */
export type ResultOfAction<Action, ResultMap = ${summary.result.name}> = Action extends { scope: infer Scope; type: infer Type }
  ? Scope extends keyof ResultMap
    ? Type extends keyof ResultMap[Scope]
      ? ResultMap[Scope][Type]
      : never
    : never
  : never
`;
}
//# sourceMappingURL=action-types.generator.js.map