import { ArrayType } from './ArrayType.js';
import { ValidationError } from '../errors.js';
function mapHydrator(items, hydrate) {
  if (items && items.length > 0 && typeof items[0] === 'number') {
    return i => +i;
  }
  return hydrate;
}
/** Maps a database array column to a JS array of enum values, with validation against allowed enum items. */
export class EnumArrayType extends ArrayType {
  owner;
  items;
  constructor(owner, items, hydrate = i => i) {
    super(mapHydrator(items, hydrate));
    this.owner = owner;
    this.items = items;
  }
  convertToDatabaseValue(value, platform, context) {
    if (Array.isArray(value) && Array.isArray(this.items)) {
      const invalid = value.filter(v => !this.items.includes(v));
      if (invalid.length > 0) {
        throw ValidationError.invalidEnumArrayItems(this.owner, invalid);
      }
    }
    return super.convertToDatabaseValue(value, platform, context);
  }
  getColumnType(prop, platform) {
    if (prop.nativeEnumName) {
      return `${prop.nativeEnumName}[]`;
    }
    return super.getColumnType(prop, platform);
  }
}
