import { Type } from './Type.js';
/** Maps a database JSON/JSONB column to a JS object, handling serialization and deserialization. */
export class JsonType extends Type {
  convertToDatabaseValue(value, platform, context) {
    if (value == null) {
      return value;
    }
    return platform.convertJsonToDatabaseValue(value, context);
  }
  convertToJSValueSQL(key, platform) {
    return key + platform.castJsonValue(this.prop);
  }
  convertToDatabaseValueSQL(key, platform) {
    return key + platform.castColumn(this.prop);
  }
  convertToJSValue(value, platform, context) {
    const isJsonColumn = ['json', 'jsonb', platform.getJsonDeclarationSQL()].includes(this.prop.columnTypes[0]);
    const isObjectEmbedded = this.prop.embedded && this.prop.object;
    if ((platform.convertsJsonAutomatically() || isObjectEmbedded) && isJsonColumn && !context?.force) {
      return value;
    }
    return platform.convertJsonToJSValue(value, context);
  }
  getColumnType(prop, platform) {
    return platform.getJsonDeclarationSQL();
  }
  ensureComparable(meta, prop) {
    return !prop.embedded || !meta.properties[prop.embedded[0]].object;
  }
  compareAsType() {
    return 'any';
  }
  get runtimeType() {
    return 'any';
  }
}
