import { ReferenceKind, Utils } from '@mikro-orm/core';
import { prepareMetadataContext } from '../utils.js';
/** Defines a scalar property on an entity (TC39 decorator). */
export function Property(options = {}) {
  return function (value, context) {
    const meta = prepareMetadataContext(context, ReferenceKind.SCALAR);
    const { check, ...opts } = options;
    const prop = { kind: ReferenceKind.SCALAR, ...opts };
    const name = options.name ?? context.name;
    meta.checks ??= [];
    if (context.name !== name) {
      Utils.renameKey(options, 'name', 'fieldName');
    }
    if (context.kind === 'field') {
      prop.name = context.name;
      prop.getter = false;
      prop.setter = false;
    } else if (context.kind === 'getter') {
      prop.name = context.name;
      prop.getter = true;
      prop.setter = false;
    } else if (context.kind === 'setter') {
      prop.name = context.name;
      prop.getter = false;
      prop.setter = true;
    } else if (context.kind === 'accessor') {
      prop.name = context.name;
      prop.getter = true;
      prop.setter = true;
    } else if (context.kind === 'method') {
      prop.getter = true;
      prop.persist = false;
      prop.type = 'method';
      prop.getterName = context.name;
      prop.name = name;
    }
    if (check) {
      meta.checks.push({ property: prop.name, expression: check });
    }
    meta.properties[prop.name] = prop;
  };
}
