"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeFile = writeFile;
exports.readFile = readFile;
exports.listFiles = listFiles;
exports.deleteFile = deleteFile;
const path = __importStar(require("path"));
const storage_1 = require("../../lib/storage");
const MIME_MAP = {
    '.json': 'application/json', '.txt': 'text/plain', '.html': 'text/html',
    '.csv': 'text/csv', '.xml': 'application/xml', '.png': 'image/png',
    '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif',
    '.pdf': 'application/pdf', '.svg': 'image/svg+xml',
};
async function writeFile(args) {
    const buffer = args.encoding === 'base64'
        ? Buffer.from(args.content, 'base64')
        : Buffer.from(args.content, 'utf-8');
    return (0, storage_1.getStorageBackend)().write(args.path, buffer);
}
async function readFile(args) {
    const { data, size } = await (0, storage_1.getStorageBackend)().read(args.path);
    const ext = path.extname(args.path).toLowerCase();
    const content = args.encoding === 'base64'
        ? data.toString('base64')
        : data.toString('utf-8');
    return {
        content,
        size,
        mime_type: MIME_MAP[ext] || 'application/octet-stream',
    };
}
async function listFiles(args) {
    const result = await (0, storage_1.getStorageBackend)().listWithPrefixes(args.directory, args.page_size || 100, args.continuation_token);
    // Apply glob pattern client-side if provided (S3/GCS don't support server-side glob)
    if (args.pattern) {
        const regex = new RegExp(args.pattern.replace(/\./g, '\\.').replace(/\*/g, '.*'));
        result.files = result.files.filter((f) => regex.test(f.path));
    }
    return result;
}
async function deleteFile(args) {
    return (0, storage_1.getStorageBackend)().delete(args.path);
}
