import { y as defineEventHandler, b0 as useLogger, aa as getRouterParam, v as createError, E as Event, d as Place, s as canModifyEvent, aG as readValidatedBody, at as notifyEvent, ao as mapEvent, c as EventStatusEnum } from '../../../nitro/nitro.mjs';
import { g as getUser } from '../../../_/getUser.mjs';
import { z } from 'zod';
import 'luxon';
import '@sequelize/core';
import 'html-to-text';
import 'crypto';
import 'node:fs';
import 'sharp';
import 'path';
import 'node:stream';
import 'jsdom';
import 'dompurify';
import 'mime-types';
import 'fs';
import 'fs/promises';
import 'linkify-html';
import '@peertube/http-signature';
import 'jsonld';
import 'lodash.intersection';
import 'email-templates';
import 'node:http';
import 'node:https';
import 'node:crypto';
import 'node:events';
import 'node:buffer';
import 'node:path';
import 'vue';
import 'vue-router';
import 'dotenv';
import 'url';
import 'bcryptjs';
import 'slugify';
import 'umzug';
import 'consola';
import '@sequelize/core/decorators-legacy';
import 'util';
import 'mitt';
import 'ical.js';
import 'node:url';
import 'fast-xml-parser';
import '../../../_/jwt.mjs';
import 'jose';

const schema = z.object({
  status: z.enum(EventStatusEnum).optional()
}).refine((data) => data.status !== void 0, {
  error: "At least one field must be provided"
});
const index_patch = defineEventHandler(async (evt) => {
  const log = useLogger("Patch event");
  const id = getRouterParam(evt, "id");
  const user = await getUser(evt);
  if (!user) {
    log.warn("Anonymous user cannot patch events");
    throw createError({ statusCode: 401, statusMessage: "Anonymous user cannot patch events" });
  }
  if (!id || !Number(id)) {
    log.warn("Event not found: %s", id);
    throw createError({ statusCode: 404, statusMessage: "Event not found" });
  }
  const event = await Event.findByPk(id, { include: [{ model: Place, required: false }] });
  if (!event) {
    log.warn("Event not found: %d", id);
    throw createError({ statusCode: 404, statusMessage: "Event not found" });
  }
  if (!canModifyEvent(event, user)) {
    log.warn("Cannot modify the event %s", event.title);
    throw createError({ statusCode: 401, statusMessage: "Cannot modify this event" });
  }
  const { success, error, data } = await readValidatedBody(evt, schema.safeParse);
  if (!success) {
    throw createError({ statusCode: 404, statusMessage: error.message });
  }
  log.info("update event status ", data.status);
  await event.update({ status: data.status });
  if (!event.recurrent && event.is_visible) {
    notifyEvent("Update", event.id);
  }
  return mapEvent(event);
});

export { index_patch as default };
