import { Type } from './Type.js';
import { ValidationError } from '../errors.js';
/** Maps a database text/array column to a JS array, using platform-specific marshalling (e.g., PostgreSQL arrays or comma-separated strings). */
export class ArrayType extends Type {
  toJsValue;
  toDbValue;
  constructor(toJsValue = i => i, toDbValue = i => i) {
    super();
    this.toJsValue = toJsValue;
    this.toDbValue = toDbValue;
  }
  convertToDatabaseValue(value, platform, context) {
    if (!value) {
      return value;
    }
    if (Array.isArray(value)) {
      return platform.marshallArray(value.map(i => this.toDbValue(i)));
    }
    /* v8 ignore next */
    if (context?.fromQuery) {
      return value;
    }
    throw ValidationError.invalidType(ArrayType, value, 'JS');
  }
  convertToJSValue(value, platform) {
    if (value == null) {
      return value;
    }
    if (typeof value === 'string') {
      value = platform.unmarshallArray(value);
    }
    return value.map(i => this.toJsValue(i));
  }
  compareAsType() {
    return 'string[]';
  }
  toJSON(value) {
    return value;
  }
  getColumnType(prop, platform) {
    return platform.getArrayDeclarationSQL();
  }
}
