import { Type } from './Type.js';
/**
 * Type that maps an SQL DECIMAL to a JS string or number.
 */
export class DecimalType extends Type {
  mode;
  constructor(mode) {
    super();
    this.mode = mode;
  }
  /* v8 ignore next */
  convertToJSValue(value) {
    if ((this.mode ?? this.prop?.runtimeType) === 'number') {
      return +value;
    }
    return String(value);
  }
  compareValues(a, b) {
    return this.format(a) === this.format(b);
  }
  format(val) {
    /* v8 ignore next */
    if (this.prop?.scale == null) {
      return +val;
    }
    const base = Math.pow(10, this.prop.scale);
    return Math.round((+val + Number.EPSILON) * base) / base;
  }
  getColumnType(prop, platform) {
    return platform.getDecimalTypeDeclarationSQL(prop);
  }
  compareAsType() {
    return this.mode ?? this.prop?.runtimeType ?? 'string';
  }
}
