import { UndefinedOr } from "../../../../utilities";
import { DomainError, handleDomainError } from "../errors";
import { RuleValidator } from "../RuleValidator";
import { Result } from "./Result";
import { IStringValueObjectOptions, StringValueObject } from "./StringValueObject";

export class Measure extends StringValueObject {
  protected static validate(value: UndefinedOr<string>, options: IStringValueObjectOptions) {
    const ruleIsEmpty = RuleValidator.RULE_ALLOW_EMPTY.default("").label(String(options.label));

    return RuleValidator.validate<string>(ruleIsEmpty, value);
  }

  public static create(value: UndefinedOr<string>, options: IStringValueObjectOptions = {}) {
    const _options = {
      label: "description",
      ...options,
    };

    const validationResult = Measure.validate(value, _options);

    if (validationResult.isFailure) {
      return Result.fail(
        handleDomainError(DomainError.INVALID_INPUT_DATA, validationResult.error.message, _options)
      );
    }

    return Result.ok(new Measure(validationResult.object));
  }
}
