import { __decorate, __metadata } from "tslib";
import DataLoader from 'dataloader';
import { Injectable, Scope } from 'graphql-modules';
import { sql } from '@pgtyped/runtime';
import { getCacheInstance } from '../../../shared/helpers/index.js';
import { DBProvider } from '../../app-providers/db.provider.js';
const getAllDepreciationCategories = sql `
  SELECT *
  FROM accounter_schema.depreciation_categories`;
const getDepreciationCategoriesByIds = sql `
  SELECT *
  FROM accounter_schema.depreciation_categories
  WHERE id IN $$depreciationCategoriesIds;`;
const updateDepreciationCategory = sql `
  UPDATE accounter_schema.depreciation_categories
  SET
  name = COALESCE(
    $name,
    name
  ),
  percentage = COALESCE(
    $percentage,
    percentage
  )
  WHERE
    id = $depreciationCategoryId
  RETURNING *;`;
const insertDepreciationCategory = sql `
  INSERT INTO accounter_schema.depreciation_categories (name, percentage)
  VALUES ($name, $percentage)
  RETURNING *`;
const deleteDepreciationCategory = sql `
  DELETE FROM accounter_schema.depreciation_categories
  WHERE id = $depreciationCategoryId
  RETURNING id;
`;
let DepreciationCategoriesProvider = class DepreciationCategoriesProvider {
    dbProvider;
    cache = getCacheInstance({
        stdTTL: 60 * 60 * 24, // 24 hours
    });
    constructor(dbProvider) {
        this.dbProvider = dbProvider;
    }
    getAllDepreciationCategories() {
        const data = this.cache.get('all-depreciation-categories');
        if (data) {
            return Promise.resolve(data);
        }
        return getAllDepreciationCategories.run(undefined, this.dbProvider).then(data => {
            this.cache.set('all-depreciation-categories', data, 60 * 60 * 24);
            return data;
        });
    }
    async batchDepreciationCategoriesByIds(depreciationCategoriesIds) {
        const categories = await getDepreciationCategoriesByIds.run({
            depreciationCategoriesIds,
        }, this.dbProvider);
        return depreciationCategoriesIds.map(id => categories.find(category => category.id === id));
    }
    getDepreciationCategoriesByIdLoader = new DataLoader((ids) => this.batchDepreciationCategoriesByIds(ids), {
        cacheKeyFn: key => `depreciation-category-${key}`,
        cacheMap: this.cache,
    });
    updateDepreciationCategory(params) {
        this.clearCache();
        return updateDepreciationCategory.run(params, this.dbProvider);
    }
    insertDepreciationCategory(params) {
        this.clearCache();
        return insertDepreciationCategory.run(params, this.dbProvider);
    }
    deleteDepreciationCategory(params) {
        this.cache.clear();
        return deleteDepreciationCategory.run(params, this.dbProvider);
    }
    clearCache() {
        this.cache.clear();
    }
};
DepreciationCategoriesProvider = __decorate([
    Injectable({
        scope: Scope.Singleton,
        global: true,
    }),
    __metadata("design:paramtypes", [DBProvider])
], DepreciationCategoriesProvider);
export { DepreciationCategoriesProvider };
//# sourceMappingURL=depreciation-categories.provider.js.map