import { Utils, ReferenceKind } from '@mikro-orm/core';
import { validateSingleDecorator, getMetadataFromDecorator } from '../utils.js';
/** Defines a scalar property on an entity (legacy TypeScript decorator). */
export function Property(options = {}) {
  return function (target, propertyName) {
    const meta = getMetadataFromDecorator(target.constructor);
    const desc = Object.getOwnPropertyDescriptor(target, propertyName) || {};
    validateSingleDecorator(meta, propertyName, ReferenceKind.SCALAR);
    const name = options.name || propertyName;
    if (propertyName !== name && !(desc.value instanceof Function)) {
      Utils.renameKey(options, 'name', 'fieldName');
    }
    options.name = propertyName;
    const { check, ...opts } = options;
    const prop = { kind: ReferenceKind.SCALAR, ...opts };
    prop.getter = !!desc.get;
    prop.setter = !!desc.set;
    if (desc.value instanceof Function) {
      prop.getter = true;
      prop.persist = false;
      prop.type = 'method';
      prop.getterName = propertyName;
      prop.name = name;
    }
    if (check) {
      meta.checks.push({ property: prop.name, expression: check });
    }
    meta.properties[prop.name] = prop;
  };
}
