import { y as defineEventHandler, b0 as useLogger, aG as readValidatedBody, T as Tag, v as createError, ar as normalizeTag } from '../../nitro/nitro.mjs';
import { z } from 'zod';
import { r as requireAdminSession } from '../../_/session.mjs';
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 '../../_/getUser.mjs';
import '../../_/jwt.mjs';
import 'jose';

const schema = z.object({
  tag: z.string(),
  newTag: z.string()
});
const index_put = defineEventHandler(async (evt) => {
  await requireAdminSession(evt);
  const log = useLogger("tag update");
  const { tag, newTag: newTagStr } = await readValidatedBody(evt, schema.parse);
  const oldtag = await Tag.findByPk(tag);
  if (!oldtag) {
    log.error("This tag does not exist: ", tag);
    throw createError({ status: 404, message: "This tag does not exists" });
  }
  const newTagNormalized = normalizeTag(newTagStr);
  const newtag = await Tag.findByPk(newTagNormalized);
  if (!newtag) {
    log.info(`Rename tag "${oldtag.tag}" to "${newTagNormalized}"`);
    await Tag.update({ tag: newTagNormalized }, { where: { tag } });
  } else {
    log.info(`Merge tag "${oldtag.tag}" to "${newTagNormalized}"`);
    const events = await oldtag.getEvents();
    await oldtag.removeEvents(events);
    await newtag.addEvents(events);
  }
});

export { index_put as default };
