import { d as defineEventHandler, dL as optionalAuth, bT as requirePermission, j as getQuery, G as createContentManager, c as createError, E as dbQuery, bN as dbQueryOne } from '../../nitro/nitro.mjs';
import { a as parsePositiveInt } from '../../_/ids.mjs';
import 'node:crypto';
import 'jsonwebtoken';
import 'node:fs';
import 'node:module';
import 'node:path';
import 'node:url';
import 'node:http';
import 'node:https';
import 'node:events';
import 'node:buffer';
import 'pino';
import 'drizzle-orm/better-sqlite3';
import 'drizzle-orm/node-postgres';
import 'drizzle-orm/mysql2';
import 'better-sqlite3';
import 'pg';
import 'mysql2/promise';
import 'drizzle-orm';
import 'fs';
import 'path';
import 'prom-client';
import 'bcryptjs';
import '@node-rs/argon2';
import 'node:async_hooks';
import 'crypto';
import 'node:dns';
import 'node:util';
import 'node:net';
import 'fs/promises';
import 'url';
import 'semver';

const index_get = defineEventHandler(async (event) => {
  try {
    await optionalAuth(event);
    await requirePermission(
      event.context.user,
      "media",
      "read",
      event.context.adminUser,
      event.context.apiToken
    );
    const query = getQuery(event);
    const folderManager = createContentManager("media-folder");
    const parentParam = query.parent;
    let folders;
    if (parentParam !== void 0 && parentParam !== "null") {
      const parentId = parsePositiveInt(parentParam);
      if (!parentId) {
        throw createError({
          statusCode: 400,
          statusMessage: "Bad Request",
          message: "Invalid parent ID"
        });
      }
      const result = await folderManager.findMany({
        filters: { parent: parentId },
        sort: "name",
        sortOrder: "asc",
        limit: 1e3
      });
      folders = result.data;
    } else {
      const allFolders = await dbQuery(
        `SELECT * FROM media_folders WHERE parent_id IS NULL ORDER BY name ASC`
      );
      folders = allFolders.map((f) => {
        var _a;
        const mapped = { ...f };
        mapped.parent = (_a = mapped.parent_id) != null ? _a : null;
        delete mapped.parent_id;
        return mapped;
      });
    }
    const enriched = await Promise.all(
      folders.map(async (folder) => {
        const folderId = folder.id;
        const childResult = await dbQueryOne(
          `SELECT COUNT(*) as count FROM media_folders WHERE parent_id = ?`,
          [folderId]
        );
        const fileResult = await dbQueryOne(
          `SELECT COUNT(*) as count FROM media WHERE folder_id = ?`,
          [folderId]
        );
        return {
          ...folder,
          childCount: Number((childResult == null ? void 0 : childResult.count) || 0),
          fileCount: Number((fileResult == null ? void 0 : fileResult.count) || 0)
        };
      })
    );
    return {
      success: true,
      data: enriched
    };
  } catch (error) {
    const err = error;
    if (err.statusCode) {
      throw error;
    }
    console.error("Get media folders error:", error);
    throw createError({
      statusCode: 500,
      statusMessage: "Internal Server Error",
      message: err.message || "Failed to get media folders"
    });
  }
});

export { index_get as default };
//# sourceMappingURL=index.get2.mjs.map
