var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import BillingService from "./BillingService";
import DatabaseService from "./DatabaseService";
import ProjectService from "./ProjectService";
import LIMIT_MAX, { LIMIT_PER_PROJECT } from "../../Types/Database/LimitMax";
import BadDataException from "../../Types/Exception/BadDataException";
import Model from "../../Models/DatabaseModels/BillingPaymentMethod";
import CaptureSpan from "../Utils/Telemetry/CaptureSpan";
export class Service extends DatabaseService {
    constructor() {
        super(Model);
    }
    async onBeforeFind(findBy) {
        if (!findBy.props.tenantId) {
            throw new BadDataException("ProjectID not found.");
        }
        const project = await ProjectService.findOneById({
            id: findBy.props.tenantId,
            props: Object.assign(Object.assign({}, findBy.props), { isRoot: true, ignoreHooks: true }),
            select: {
                _id: true,
                paymentProviderCustomerId: true,
            },
        });
        if (!project) {
            throw new BadDataException("Project not found");
        }
        if (!project.paymentProviderCustomerId) {
            throw new BadDataException("Payment provider customer id not found.");
        }
        const paymentMethods = await BillingService.getPaymentMethods(project.paymentProviderCustomerId);
        await this.deleteBy({
            query: {
                projectId: findBy.props.tenantId,
            },
            limit: LIMIT_PER_PROJECT,
            skip: 0,
            props: {
                isRoot: true,
                ignoreHooks: true,
            },
        });
        for (const paymentMethod of paymentMethods) {
            const billingPaymentMethod = new Model();
            billingPaymentMethod.projectId = project.id;
            billingPaymentMethod.paymentMethodType = paymentMethod.type;
            billingPaymentMethod.last4Digits = paymentMethod.last4Digits;
            billingPaymentMethod.isDefault = paymentMethod.isDefault;
            billingPaymentMethod.paymentProviderPaymentMethodId = paymentMethod.id;
            billingPaymentMethod.paymentProviderCustomerId =
                project.paymentProviderCustomerId;
            await this.create({
                data: billingPaymentMethod,
                props: {
                    isRoot: true,
                },
            });
        }
        return { findBy, carryForward: paymentMethods };
    }
    async onBeforeDelete(deleteBy) {
        const items = await this.findBy({
            query: deleteBy.query,
            select: {
                _id: true,
                paymentProviderPaymentMethodId: true,
                paymentProviderCustomerId: true,
            },
            skip: 0,
            limit: LIMIT_MAX,
            props: {
                isRoot: true,
                ignoreHooks: true,
            },
        });
        for (const item of items) {
            if (item.paymentProviderPaymentMethodId &&
                item.paymentProviderCustomerId) {
                await BillingService.deletePaymentMethod(item.paymentProviderCustomerId, item.paymentProviderPaymentMethodId);
            }
        }
        return { deleteBy, carryForward: null };
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "onBeforeFind", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "onBeforeDelete", null);
export default new Service();
//# sourceMappingURL=BillingPaymentMethodService.js.map