var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import fs from "fs";
import CaptureSpan from "./Telemetry/CaptureSpan";
export default class LocalFile {
    static async copyDirectory(data) {
        const source = data.source;
        const destination = data.destination;
        // copy source to destination recursively
        return new Promise((resolve, reject) => {
            fs.cp(source, destination, { recursive: true }, (err) => {
                if (err) {
                    return reject(err);
                }
                resolve();
            });
        });
    }
    static async copyFile(data) {
        return new Promise((resolve, reject) => {
            fs.copyFile(data.source, data.destination, (err) => {
                if (err) {
                    return reject(err);
                }
                resolve();
            });
        });
    }
    static sanitizeFilePath(filePath) {
        // remove double slashes
        return filePath.replace(/\/\//g, "/");
    }
    static getFileExtension(filePath) {
        var _a;
        const fileExtention = filePath.split(".");
        return ((_a = fileExtention[fileExtention.length - 1]) === null || _a === void 0 ? void 0 : _a.toLowerCase()) || "";
    }
    static async deleteFile(filePath) {
        if ((await this.doesFileExist(filePath)) === false) {
            return;
        }
        return new Promise((resolve, reject) => {
            fs.unlink(filePath, (err) => {
                if (err) {
                    return reject(err);
                }
                resolve();
            });
        });
    }
    static async deleteDirectory(path) {
        if ((await this.doesDirectoryExist(path)) === false) {
            return;
        }
        return new Promise((resolve, reject) => {
            fs.rm(path, { recursive: true, force: true }, (err) => {
                if (err) {
                    return reject(err);
                }
                resolve();
            });
        });
    }
    static async readDirectory(path) {
        return new Promise((resolve, reject) => {
            fs.readdir(path, { withFileTypes: true }, (err, entries) => {
                if (err) {
                    return reject(err);
                }
                resolve(entries);
            });
        });
    }
    static async getListOfDirectories(path) {
        return new Promise((resolve, reject) => {
            fs.readdir(path, { withFileTypes: true }, (err, files) => {
                if (err) {
                    return reject(err);
                }
                const directories = files
                    .filter((file) => {
                    return file.isDirectory();
                })
                    .map((file) => {
                    return file.name;
                });
                resolve(directories);
            });
        });
    }
    static async doesFileExist(path) {
        return new Promise((resolve, reject) => {
            fs.stat(path, (err, stats) => {
                if (err) {
                    if (err.code === "ENOENT") {
                        return resolve(false);
                    }
                    return reject(err);
                }
                if (stats.isFile()) {
                    return resolve(true);
                }
                return resolve(false);
            });
        });
    }
    static async doesDirectoryExist(path) {
        return new Promise((resolve, reject) => {
            fs.stat(path, (err, stats) => {
                if (err) {
                    if (err.code === "ENOENT") {
                        return resolve(false);
                    }
                    return reject(err);
                }
                if (stats.isDirectory()) {
                    return resolve(true);
                }
                return resolve(false);
            });
        });
    }
    static async deleteAllDataInDirectory(path) {
        if ((await this.doesDirectoryExist(path)) === false) {
            return;
        }
        return new Promise((resolve, reject) => {
            fs.rm(path, { recursive: true }, (err) => {
                if (err) {
                    return reject(err);
                }
                // now crate the directory again
                fs.mkdir(path, { recursive: true }, (err) => {
                    if (err) {
                        return reject(err);
                    }
                    resolve();
                });
            });
        });
    }
    static async makeDirectory(path) {
        return new Promise((resolve, reject) => {
            fs.mkdir(path, { recursive: true }, (err) => {
                if (err) {
                    return reject(err);
                }
                resolve();
            });
        });
    }
    static async write(path, data) {
        return new Promise((resolve, reject) => {
            fs.writeFile(path, data, (err) => {
                if (err) {
                    return reject(err);
                }
                resolve();
            });
        });
    }
    static async read(path) {
        return new Promise((resolve, reject) => {
            fs.readFile(path, { encoding: "utf-8" }, (err, data) => {
                if (!err) {
                    return resolve(data);
                }
                return reject(err);
            });
        });
    }
    static async readAsBuffer(path) {
        return new Promise((resolve, reject) => {
            fs.readFile(path, (err, data) => {
                if (!err) {
                    return resolve(data);
                }
                return reject(err);
            });
        });
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], LocalFile, "copyDirectory", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], LocalFile, "copyFile", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", String)
], LocalFile, "sanitizeFilePath", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", String)
], LocalFile, "getFileExtension", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], LocalFile, "deleteFile", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], LocalFile, "deleteDirectory", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], LocalFile, "readDirectory", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], LocalFile, "getListOfDirectories", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], LocalFile, "doesFileExist", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], LocalFile, "doesDirectoryExist", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], LocalFile, "deleteAllDataInDirectory", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], LocalFile, "makeDirectory", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", Promise)
], LocalFile, "write", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], LocalFile, "read", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], LocalFile, "readAsBuffer", null);
//# sourceMappingURL=LocalFile.js.map