/* eslint-disable @typescript-eslint/naming-convention */
/* eslint-disable @typescript-eslint/explicit-function-return-type */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { EntityStore } from '../EntityStore';

export const loadable = (
  target: EntityStore<any>,
  key: string,
  descriptor: any,
) => {
  const originalMethod = descriptor.value;

  descriptor.value = async function (...args: any[]) {
    const _this = this as EntityStore<any>;
    try {
      // if (_this.loading) return;
      _this.setLoading(true);
      await originalMethod.call(_this, ...args);
    } finally {
      _this.setLoading(false);
    }
  };
};

export const loadableCard = (
  target: EntityStore<any>,
  key: string,
  descriptor: any,
) => {
  const originalMethod = descriptor.value;

  descriptor.value = async function (...args: any[]) {
    const _this = this as EntityStore<any>;
    try {
      _this.setLoadingCard(true);
      await originalMethod.call(_this, ...args);
    } finally {
      _this.setLoadingCard(false);
    }
  };
};
