"use strict";
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);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
var Access_1;
Object.defineProperty(exports, "__esModule", { value: true });
const typeorm_1 = require("typeorm");
const __1 = require("..");
const users_1 = __importDefault(require("./users"));
const access_fields_1 = __importDefault(require("./access-fields"));
const helpers_1 = require("../../helpers");
const providers_1 = require("../../providers");
const helpers_2 = require("../helpers");
let Access = Access_1 = class Access {
    constructor() {
        // Text status indicating whether the last poll was successful or not.
        this.fetchStatus = helpers_1.FETCH_STATUS_SUCCESS;
        // Text label set by the user.
        this.customLabel = null;
        // A JSON-serialized session's content.
        this.session = null;
        // Whether the access should not be polled regularly (for instance, because it always requires
        // an interactive authentication).
        this.excludeFromPoll = false;
    }
    static repo() {
        if (Access_1.REPO === null) {
            Access_1.REPO = (0, __1.getRepository)(Access_1);
        }
        return Access_1.REPO;
    }
    // Entity methods.
    // Helper to get a field value by name
    getFieldValue(fieldName) {
        var _a, _b;
        const field = (_a = this.fields) === null || _a === void 0 ? void 0 : _a.find(f => f.name === fieldName);
        return (_b = field === null || field === void 0 ? void 0 : field.value) !== null && _b !== void 0 ? _b : null;
    }
    // Is the access enabled?
    isEnabled() {
        return (0, helpers_2.areFieldsComplete)(this.vendorId, this.fields);
    }
    // Returns a cleaned up label for this access.
    getLabel() {
        if (this.customLabel) {
            return this.customLabel;
        }
        return (0, providers_1.bankVendorByUuid)(this.vendorId).name;
    }
    // Can the access be polled?
    canBePolled() {
        return (!this.excludeFromPoll &&
            this.isEnabled() &&
            this.fetchStatus !== 'INVALID_PASSWORD' &&
            this.fetchStatus !== 'EXPIRED_PASSWORD' &&
            this.fetchStatus !== 'INVALID_PARAMETERS' &&
            this.fetchStatus !== 'NO_PASSWORD' &&
            this.fetchStatus !== 'MISSING_MANDATORY_FIELD' &&
            this.fetchStatus !== 'ACTION_NEEDED' &&
            this.fetchStatus !== 'AUTH_METHOD_NYI' &&
            this.fetchStatus !== 'REQUIRES_INTERACTIVE');
    }
    // Doesn't insert anything in db, only creates a new instance and normalizes its fields.
    static cast(args) {
        return Access_1.repo().create(args);
    }
    static async create(userId, { fields = [], ...other }) {
        const fieldsWithUserId = fields.map(field => ({
            ...field,
            userId,
        }));
        const entity = Access_1.repo().create({
            ...other,
            userId,
            fields: fieldsWithUserId,
        });
        const access = await Access_1.repo().save(entity);
        return access;
    }
    static async find(userId, accessId) {
        return await Access_1.repo().findOne({
            where: { userId, id: accessId },
            relations: ['fields'],
        });
    }
    static async all(userId) {
        return await Access_1.repo().find({
            where: { userId },
            relations: ['fields'],
        });
    }
    static async exists(userId, accessId) {
        const found = await Access_1.repo().findOne({
            where: { userId, id: accessId },
        });
        return !!found;
    }
    static async destroy(userId, accessId) {
        await Access_1.repo().delete({ userId, id: accessId });
    }
    static async destroyAll(userId) {
        await Access_1.repo().delete({ userId });
    }
    static async update(userId, accessId, newAttributes) {
        if (Object.keys(newAttributes).length > 0) {
            await Access_1.repo().update({ userId, id: accessId }, newAttributes);
        }
        return (0, helpers_1.unwrap)(await Access_1.find(userId, accessId));
    }
    static async byVendorId(userId, { uuid: vendorId }) {
        return await Access_1.repo().find({
            where: { userId, vendorId },
            relations: ['fields'],
        });
    }
    static async byCredentials(userId, { uuid, login }) {
        const accesses = await Access_1.byVendorId(userId, { uuid });
        // Find the one with the matching login field
        return accesses.find(access => access.getFieldValue('login') === login) || null;
    }
};
Access.REPO = null;
// Static attributes.
Access.renamings = {
    bank: 'vendorId',
};
__decorate([
    (0, typeorm_1.PrimaryGeneratedColumn)(),
    __metadata("design:type", Number)
], Access.prototype, "id", void 0);
__decorate([
    (0, typeorm_1.ManyToOne)(() => users_1.default, {
        cascade: true,
        onDelete: 'CASCADE',
        nullable: false,
    }),
    (0, typeorm_1.JoinColumn)(),
    __metadata("design:type", users_1.default)
], Access.prototype, "user", void 0);
__decorate([
    (0, typeorm_1.Column)('integer'),
    __metadata("design:type", Number)
], Access.prototype, "userId", void 0);
__decorate([
    (0, typeorm_1.Column)('varchar'),
    __metadata("design:type", String)
], Access.prototype, "vendorId", void 0);
__decorate([
    (0, typeorm_1.Column)('varchar', { default: helpers_1.FETCH_STATUS_SUCCESS }),
    __metadata("design:type", String)
], Access.prototype, "fetchStatus", void 0);
__decorate([
    (0, typeorm_1.Column)('varchar', { nullable: true, default: null }),
    __metadata("design:type", Object)
], Access.prototype, "customLabel", void 0);
__decorate([
    (0, typeorm_1.OneToMany)(() => access_fields_1.default, accessField => accessField.access, {
        cascade: ['insert'],
    }),
    __metadata("design:type", Array)
], Access.prototype, "fields", void 0);
__decorate([
    (0, typeorm_1.Column)('varchar', { nullable: true, default: null }),
    __metadata("design:type", Object)
], Access.prototype, "session", void 0);
__decorate([
    (0, typeorm_1.Column)('boolean', { default: false }),
    __metadata("design:type", Object)
], Access.prototype, "excludeFromPoll", void 0);
Access = Access_1 = __decorate([
    (0, typeorm_1.Entity)('access')
], Access);
exports.default = Access;
