import { ReflectUtils } from "@dooboostore/core/reflect/ReflectUtils";
const ATTRIBUTE_METADATA_KEY = Symbol("simple-web-component:attribute");
function attribute(arg1, arg2) {
  const decorator = (options, target, propertyKey) => {
    const constructor = target.constructor;
    if (!options.name) options.name = String(propertyKey);
    if (!options.type) options.type = String;
    if (options.disableReflect === void 0) options.disableReflect = false;
    let list = ReflectUtils.getMetadata(ATTRIBUTE_METADATA_KEY, constructor);
    if (!list) {
      list = [];
      ReflectUtils.defineMetadata(ATTRIBUTE_METADATA_KEY, list, constructor);
    }
    list.push({ propertyKey, options });
    const internalKey = Symbol(String(propertyKey));
    Object.defineProperty(target, propertyKey, {
      get() {
        return this[internalKey];
      },
      set(val) {
        const oldVal = this[internalKey];
        if (oldVal === val) return;
        let convertedVal = val;
        if (options.type === Number) convertedVal = Number(val);
        else if (options.type === Boolean) convertedVal = Boolean(val);
        else if (options.type === Object && typeof val === "string") {
          try {
            convertedVal = JSON.parse(val);
          } catch (e) {
            convertedVal = val;
          }
        }
        this[internalKey] = convertedVal;
        if (typeof this._updateState === "function") {
          this._updateState(options.name);
        }
        if (!options.disableReflect && this.setAttribute) {
          if (convertedVal === null || convertedVal === void 0 || convertedVal === false) {
            this.removeAttribute(options.name);
          } else {
            const attrVal = typeof convertedVal === "object" ? JSON.stringify(convertedVal) : String(convertedVal);
            if (this.getAttribute(options.name) !== attrVal) {
              this.setAttribute(options.name, attrVal);
            }
          }
        }
      },
      enumerable: true,
      configurable: true
    });
  };
  if (arg1 && typeof arg2 === "string") {
    return decorator({}, arg1, arg2);
  }
  return (target, propertyKey) => {
    const options = typeof arg1 === "string" ? { name: arg1 } : arg1 || {};
    decorator(options, target, propertyKey);
  };
}
const getAttributeMetadataList = (target) => {
  const constructor = target instanceof Function ? target : target.constructor;
  return ReflectUtils.getMetadata(ATTRIBUTE_METADATA_KEY, constructor);
};
export {
  ATTRIBUTE_METADATA_KEY,
  attribute,
  getAttributeMetadataList
};
//# sourceMappingURL=attribute.js.map
