import { config } from "@/config";
import { IValueObjectOptions, Result, ValueObject } from "@shared/contexts";
import fs from "fs/promises";
import mime from "mime-types";

export interface IProfileLogotypeOptions extends IValueObjectOptions {}

export class ProfileLogotype extends ValueObject<string> {
  public static create(value: string, options: IProfileLogotypeOptions = {}) {
    const _options = {
      label: "logotype",
      ...options,
    };
    //const validationResult = ProfileStatus.validate(status, _options);

    /*if (validationResult.isFailure) {
          return Result.fail(
            handleDomainError(DomainError.INVALID_INPUT_DATA, validationResult.error.message, _options)
          );
        }*/
    return Result.ok(new ProfileLogotype(value));
  }

  get filename(): string {
    return this.props;
  }

  public async toBase64() {
    let data: any;
    try {
      data = await fs.readFile(this.relativePath());
    } catch (err) {
      data = await fs.readFile(config.defaults.dealer_logo_placeholder);
    }
    const mimeType = mime.lookup(data);
    return `data:${mimeType};base64,${data.toString("base64")}`;
  }

  public relativePath() {
    return this.filename
      ? `${config.defaults.dealer_logos_upload_path}/${this.filename}`
      : config.defaults.dealer_logo_placeholder;
  }

  public toPrimitive(): string {
    return this.filename;
  }
}
