import { Storage } from '@google-cloud/storage';
import { fa as StorageProvider } from '../nitro/nitro.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';

var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
class GCSStorageProvider extends StorageProvider {
  constructor(config) {
    super(config);
    __publicField(this, "storage");
    __publicField(this, "bucketName");
    __publicField(this, "publicUrl");
    __publicField(this, "defaultSignedUrlExpiry");
    if (!config.bucket) {
      throw new Error('GCS storage provider requires a "bucket" configuration');
    }
    this.bucketName = config.bucket;
    this.publicUrl = config.publicUrl;
    this.defaultSignedUrlExpiry = config.signedUrlExpiry || 3600;
    const storageOptions = {};
    if (config.projectId) {
      storageOptions.projectId = config.projectId;
    }
    if (config.keyFilename) {
      storageOptions.keyFilename = config.keyFilename;
    }
    if (config.credentials) {
      storageOptions.credentials = config.credentials;
    }
    this.storage = new Storage(storageOptions);
  }
  async upload(fileData, options) {
    const buffer = typeof fileData === "string" ? Buffer.from(fileData, "base64") : fileData;
    const mimeType = this.resolveUploadMimeType(options);
    this.validateFile(buffer.length, mimeType, options);
    const filename = options.filename ? this.sanitizeFilename(options.filename) : this.generateUniqueFilename("file");
    const folder = this.applyTenantPrefix(options);
    const key = folder ? `${folder.replace(/\\/g, "/")}/${filename}` : filename;
    const file = this.storage.bucket(this.bucketName).file(key);
    await file.save(buffer, {
      contentType: mimeType,
      resumable: false,
      metadata: {
        contentType: mimeType
      }
    });
    return {
      filename,
      originalName: options.filename || filename,
      mimeType,
      size: buffer.length,
      path: key,
      url: this.getUrl(key)
    };
  }
  async delete(options) {
    try {
      await this.storage.bucket(this.bucketName).file(options.path).delete();
    } catch (error) {
      const err = error;
      if (err.code !== 404) {
        throw error;
      }
    }
  }
  async exists(path) {
    const [exists] = await this.storage.bucket(this.bucketName).file(path).exists();
    return exists;
  }
  getUrl(path) {
    const normalizedPath = path.replace(/\\/g, "/");
    if (this.publicUrl) {
      return `${this.publicUrl.replace(/\/$/, "")}/${normalizedPath}`;
    }
    return `https://storage.googleapis.com/${this.bucketName}/${normalizedPath}`;
  }
  async getFile(path) {
    try {
      const [buffer] = await this.storage.bucket(this.bucketName).file(path).download();
      return buffer;
    } catch (error) {
      const err = error;
      if (err.code === 404) {
        throw new Error(`File not found in GCS: ${path}`);
      }
      throw error;
    }
  }
  supportsSignedUrls() {
    return true;
  }
  async getSignedUrl(path, options) {
    const expiry = this.normalizeSignedUrlExpiry(options == null ? void 0 : options.expiresIn, this.defaultSignedUrlExpiry);
    const extensionHeaders = {};
    if (options == null ? void 0 : options.responseContentDisposition) {
      extensionHeaders["response-content-disposition"] = options.responseContentDisposition;
    }
    const [url] = await this.storage.bucket(this.bucketName).file(path).getSignedUrl({
      version: "v4",
      action: "read",
      expires: Date.now() + expiry * 1e3,
      ...Object.keys(extensionHeaders).length > 0 && {
        extensionHeaders
      }
    });
    return url;
  }
  async getSignedUploadUrl(key, contentType, expiresIn) {
    const expiry = this.normalizeSignedUrlExpiry(expiresIn, this.defaultSignedUrlExpiry);
    const [url] = await this.storage.bucket(this.bucketName).file(key).getSignedUrl({
      version: "v4",
      action: "write",
      expires: Date.now() + expiry * 1e3,
      contentType
    });
    return url;
  }
}

export { GCSStorageProvider };
//# sourceMappingURL=GCSStorageProvider.mjs.map
