import fs from "fs";
import { InputOutput } from "../../interfaces/IInputOutput";

export class FileSystemService implements InputOutput {
  name: string;
  path: string;
  #fullPath:string;
  constructor(name: string, path: string) {
    this.name = name;
    this.path = path;
    this.#fullPath =this.path + "/" + this.name;
  }
  async read(): Promise<String> {
    let content: string = "";
    await fs.readFile(
      this.#fullPath,
      "utf8",
      (err: any, data: any) => {
        if (err) throw err;
        content = data;
        // console.log("Read to " + this.name + " successfully");
      }
    );
    return content;
  }
  async write(data: string): Promise<void> {
    fs.writeFile(this.#fullPath, data, (err: any) => {
      if (err) throw err;
      // console.log("Wrote to " + this.name + " successfully");
    });
  }
  getFullPath() {
    return this.#fullPath;
  }
}
