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 DatabaseConfig from "../DatabaseConfig";
import DatabaseService from "./DatabaseService";
import { LinkShortenerRoute } from "../../ServiceRoute";
import Route from "../../Types/API/Route";
import URL from "../../Types/API/URL";
import Text from "../../Types/Text";
import Model from "../../Models/DatabaseModels/ShortLink";
import { IsBillingEnabled } from "../EnvironmentConfig";
import CaptureSpan from "../Utils/Telemetry/CaptureSpan";
export class Service extends DatabaseService {
    constructor() {
        super(Model);
        if (IsBillingEnabled) {
            this.hardDeleteItemsOlderThanInDays("createdAt", 3); //expire links in 3 days.
        }
    }
    async onBeforeCreate(createBy) {
        createBy.data.shortId = Text.generateRandomText(8);
        return { createBy: createBy, carryForward: [] };
    }
    async saveShortLinkFor(url) {
        const model = new Model();
        model.link = url;
        return await this.create({ data: model, props: { isRoot: true } });
    }
    async getShortenedUrl(model) {
        var _a;
        const host = await DatabaseConfig.getHost();
        const httpProtocol = await DatabaseConfig.getHttpProtocol();
        return new URL(httpProtocol, host, new Route(LinkShortenerRoute.toString()).addRoute("/" + ((_a = model.shortId) === null || _a === void 0 ? void 0 : _a.toString())));
    }
    async getShortLinkFor(shortLinkId) {
        return await this.findOneBy({
            query: {
                shortId: shortLinkId,
            },
            select: {
                _id: true,
                link: true,
            },
            props: {
                isRoot: true,
            },
        });
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "onBeforeCreate", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [URL]),
    __metadata("design:returntype", Promise)
], Service.prototype, "saveShortLinkFor", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Model]),
    __metadata("design:returntype", Promise)
], Service.prototype, "getShortenedUrl", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], Service.prototype, "getShortLinkFor", null);
export default new Service();
//# sourceMappingURL=ShortLinkService.js.map