import { emitView } from "./view-emitter.js";
import { composeListBody } from "./list-body-composer.js";
import { composeDetailBody } from "./detail-body-composer.js";
import { composeFormBody } from "./form-body-composer.js";
import { composeDashboardBody } from "./dashboard-body-composer.js";
import { emitEntityDisplay, emitUseResizableSidebar, emitUseAppEvents } from "./helpers-emitter.js";
import { emitFieldInput, emitOperationExecutor, emitOperationResultView } from "./operation-emitters.js";
import { collectOperationViews, emitOperationView } from "./operation-view-generator.js";
const PRIMARY_VIEW_TYPES = ["list", "detail", "form", "dashboard"];
const COMPOSER_BY_TYPE = {
  list: composeListBody,
  detail: composeDetailBody,
  form: composeFormBody,
  dashboard: composeDashboardBody
};
async function generate(context) {
  const files = {};
  const spec = context.spec;
  const models = spec.models ?? {};
  const viewsBySpec = spec.views ?? {};
  const viewIndex = indexViews(viewsBySpec);
  const skippedSpecialists = [];
  for (const [modelName, model] of Object.entries(models)) {
    for (const viewType of PRIMARY_VIEW_TYPES) {
      const viewName = `${modelName}${capitalize(viewType)}View`;
      const view = viewIndex.get(viewName) ?? synthesizeDefault(modelName, viewType);
      const source = emitView({
        view,
        viewName,
        model,
        modelSchemas: models,
        renderBody: COMPOSER_BY_TYPE[viewType]
      });
      files[`src/views/${viewName}.tsx`] = source;
    }
  }
  for (const [name, view] of Object.entries(viewsBySpec)) {
    if (!PRIMARY_VIEW_TYPES.includes(view.type)) {
      skippedSpecialists.push(`${name} (type=${view.type})`);
    }
  }
  if (skippedSpecialists.length > 0) {
    console.warn(
      `[ReactAppStarter] Skipped ${skippedSpecialists.length} specialist view(s) \u2014 implement composers for: ${[...new Set(Object.values(viewsBySpec).filter((v) => !PRIMARY_VIEW_TYPES.includes(v.type)).map((v) => v.type))].join(", ")}. Skipped: ${skippedSpecialists.join(", ")}`
    );
  }
  const operationViews = collectOperationViews(spec);
  for (const desc of operationViews) {
    files[`src/views/${desc.viewName}.tsx`] = emitOperationView(desc);
  }
  files["src/lib/entity-display.ts"] = emitEntityDisplay();
  files["src/hooks/useResizableSidebar.ts"] = emitUseResizableSidebar();
  files["src/hooks/useAppEvents.tsx"] = emitUseAppEvents();
  if (operationViews.length > 0) {
    files["src/lib/FieldInput.tsx"] = emitFieldInput();
    files["src/lib/OperationExecutor.tsx"] = emitOperationExecutor();
    files["src/lib/OperationResultView.tsx"] = emitOperationResultView();
  }
  return files;
}
function indexViews(views) {
  const out = /* @__PURE__ */ new Map();
  for (const [name, view] of Object.entries(views)) {
    out.set(name, view);
  }
  return out;
}
function synthesizeDefault(modelName, viewType) {
  return {
    type: viewType,
    model: modelName
  };
}
function capitalize(s) {
  return s.charAt(0).toUpperCase() + s.slice(1);
}
export {
  generate
};
