import { validatePluginOptions } from "./options.js";
import { getRoutes as getFileUploadStatusRoutes } from "./routes/file-upload.js";
import { makeLoadFormPreHandler } from "./routes/index.js";
import { getRoutes as getPaymentRoutes } from "./routes/payment.js";
import { getRoutes as getQuestionRoutes } from "./routes/questions.js";
import { getRoutes as getRepeaterItemDeleteRoutes } from "./routes/repeaters/item-delete.js";
import { getRoutes as getRepeaterSummaryRoutes } from "./routes/repeaters/summary.js";
import { registerUnavailableResponse } from "./unavailable-response.js";
import { registerVision } from "./vision.js";
import { mapPlugin } from "../map/index.js";
import { postcodeLookupPlugin } from "../postcode-lookup/index.js";
import { CacheService } from "../../services/index.js";
export const plugin = {
  name: '@defra/forms-engine-plugin',
  dependencies: ['@hapi/crumb', '@hapi/yar', 'hapi-pino'],
  multiple: true,
  async register(server, options) {
    options = validatePluginOptions(options);
    const {
      model,
      cache,
      saveAndExit,
      nunjucks: nunjucksOptions,
      viewContext,
      preparePageEventRequestOptions,
      onRequest,
      ordnanceSurveyApiKey,
      baseUrl,
      ordnanceSurveyApiSecret,
      services
    } = options;
    const cacheService = typeof cache === 'string' ? new CacheService({
      server,
      cacheName: cache
    }) : cache;
    await registerVision(server, options);

    // Register the postcode lookup plugin only if we have an OS api key
    if (ordnanceSurveyApiKey) {
      await server.register({
        plugin: postcodeLookupPlugin,
        options: {
          ordnanceSurveyApiKey
        }
      });
    }

    // Register the maps plugin only if we have an OS api key & secret
    if (ordnanceSurveyApiKey && ordnanceSurveyApiSecret) {
      await server.register({
        plugin: mapPlugin,
        options: {
          ordnanceSurveyApiKey,
          ordnanceSurveyApiSecret
        }
      });
    }
    server.expose('baseLayoutPath', nunjucksOptions.baseLayoutPath);
    server.expose('viewContext', viewContext);
    server.expose('cacheService', cacheService);
    server.expose('saveAndExit', saveAndExit);
    server.expose('baseUrl', baseUrl);
    server.expose('services', services);
    server.app.model = model;

    // In-memory cache of FormModel items, exposed
    // (for testing purposes) through `server.app.models`
    const itemCache = new Map();
    server.app.models = itemCache;
    const loadFormPreHandler = makeLoadFormPreHandler(server, options);
    const getRouteOptions = {
      pre: [{
        method: loadFormPreHandler
      }]
    };
    const postRouteOptions = {
      payload: {
        parse: true
      },
      pre: [{
        method: loadFormPreHandler
      }]
    };
    const routes = [...getPaymentRoutes(), ...getFileUploadStatusRoutes(), ...getRepeaterSummaryRoutes(getRouteOptions, postRouteOptions, onRequest), ...getRepeaterItemDeleteRoutes(getRouteOptions, postRouteOptions, onRequest), ...getQuestionRoutes(getRouteOptions, postRouteOptions, preparePageEventRequestOptions, onRequest)];
    server.route(routes); // TODO

    // Registration order is important: must be registered after the engine's
    // routes so it sees their responses, but before any global error-page
    // handler that would re-shape Boom errors.
    registerUnavailableResponse(server);
  }
};
//# sourceMappingURL=plugin.js.map