"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var webhook_exports = {};
__export(webhook_exports, {
  webhookHandler: () => webhookHandler
});
module.exports = __toCommonJS(webhook_exports);
var import_ts_pattern = require("ts-pattern");
var import_request_response = require("../../request-response");
var import_types = require("./types");
var import_site_published = require("./site-published");
async function webhookHandler(req, { apiKey, events, revalidate }) {
  const secret = (0, import_request_response.searchParams)(req).get("secret");
  if (secret !== apiKey) {
    return import_request_response.ApiResponse.json({ message: "Unauthorized" }, { status: 401 });
  }
  let payload;
  try {
    const body = await req.json();
    payload = import_types.sitePublishedWebhookPayloadSchema.parse(body);
  } catch (error) {
    console.error(error);
    return import_request_response.ApiResponse.json({ message: "Invalid request body" }, { status: 400 });
  }
  const result = await (0, import_ts_pattern.match)(payload.type).with(
    import_types.WebhookEventType.SITE_PUBLISHED,
    () => (0, import_site_published.handleSitePublished)(payload, { onPublish: events?.onPublish, revalidate })
  ).exhaustive();
  return import_request_response.ApiResponse.json(result.body, { status: result.status });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  webhookHandler
});
//# sourceMappingURL=index.js.map