"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateUpdateService = generateUpdateService;
const Generator = __importStar(require("@postxl/generator"));
function generateUpdateService(context) {
    const imports = Generator.ImportGenerator.from(context.update.service.location).addType(context.actions.execution.interface);
    const constructor = [];
    const modelActions = [];
    const modelResultTypes = [];
    const dispatchCases = [];
    for (const model of Array.from(context.models.values()).toSorted((a, b) => a.name.localeCompare(b.name))) {
        if (model.update.kind === 'skipped') {
            continue;
        }
        imports.add(model.update.service);
        imports.addType(model.update.action.type);
        imports.addType(model.update.action.resultTypeMap);
        imports.addType(context.actions.execution.interface);
        constructor.push(`@Inject(forwardRef(() => ${model.update.service.name}))  public readonly ${model.update.service.variableName} :${model.update.service.name}`);
        modelActions.push(model.update.action.type.name);
        modelResultTypes.push(`${model.update.action.scope}: ${model.update.action.resultTypeMap.name}`);
        dispatchCases.push(`
      case '${model.update.action.scope}': return this.${model.update.service.variableName}.dispatch({ action, execution }) as Promise<Result>`);
    }
    return /* ts */ `
  import { Inject, Injectable, forwardRef } from '@nestjs/common'
  import { ExhaustiveSwitchCheck } from '@postxl/utils'

  ${imports.generate()}

  export type ${context.update.actions.summary.action.name} = ${modelActions.join(' | ')}

  export type ${context.update.actions.summary.result.name} = {
    ${modelResultTypes.join(', \n')}
  }

  @Injectable()
  export class ${context.update.service.name} {
    constructor(${constructor.join(',\n')}) {}
  
    public async dispatch<
      Action extends ${context.update.actions.summary.action.name},
      Result = ${context.update.actions.summary.result.name}[Action['scope']][keyof ${context.update.actions.summary.result.name}[Action['scope']]]
    >({
      action,
      execution,
    }: {
      action: Action
      execution: ${context.actions.execution.interface.name}
    }): Promise<Result> {
      switch (action.scope) {
        ${dispatchCases.join('\n')}
        
        default:
          throw new ExhaustiveSwitchCheck(action)
      }
    }
  }
  `;
}
//# sourceMappingURL=update-service.generator.js.map