"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculateProfileCompletion = calculateProfileCompletion;
const client_1 = require("@prisma/client");
const residentRequirementLabels = {
    firstName: 'First name',
    lastName: 'Last name',
    phone: 'Phone number',
    blockNumber: 'Block number',
    flatNumber: 'Flat number',
    address: 'Address',
    estateId: 'Estate assignment',
    profileConfirmed: 'Profile confirmation'
};
const guardRequirementLabels = {
    firstName: 'First name',
    lastName: 'Last name',
    phone: 'Phone number',
    guardCode: 'Guard ID',
    assignedGateId: 'Assigned gate',
    assignedShifts: 'Assigned shifts',
    profilePictureUrl: 'Profile photo',
    address: 'Address',
    emergencyContact: 'Emergency contact',
    estateId: 'Estate assignment',
    profileConfirmed: 'Profile confirmation'
};
const defaultResidentRequiredFields = [
    'firstName',
    'lastName',
    'phone',
    'blockNumber',
    'flatNumber',
    'address',
    'estateId',
    'profileConfirmed'
];
const defaultGuardRequiredFields = [
    'firstName',
    'lastName',
    'phone',
    'guardCode',
    'assignedGateId',
    'assignedShifts',
    'profilePictureUrl',
    'address',
    'emergencyContact',
    'estateId',
    'profileConfirmed'
];
function calculateProfileCompletion(role, profile, options = {}) {
    if (role === client_1.UserRole.RESIDENT) {
        return calculateRequiredCompletion(resolveResidentRequirements(profile, options.residentRequiredFields), residentRequirementLabels);
    }
    if (role === client_1.UserRole.GUARD) {
        return calculateRequiredCompletion(resolveGuardRequirements(profile, options.guardRequiredFields), guardRequirementLabels);
    }
    return { completion: 100, missing: [], required: [], complete: true };
}
function resolveResidentRequirements(profile, fields = defaultResidentRequiredFields) {
    return fields.map((field) => {
        if (field === 'flatNumber') {
            return [field, profile.flatNumber || profile.unit];
        }
        if (field === 'profileConfirmed') {
            return [field, profile.profileConfirmed ? 'confirmed' : null];
        }
        return [field, profile[field]];
    });
}
function resolveGuardRequirements(profile, fields = defaultGuardRequiredFields) {
    return fields.map((field) => {
        if (field === 'assignedShifts') {
            return [field, hasListValue(profile.assignedShifts) ? 'assigned' : null];
        }
        if (field === 'emergencyContact') {
            return [field, hasObjectValue(profile.emergencyContact) ? 'provided' : null];
        }
        if (field === 'profileConfirmed') {
            return [field, profile.profileConfirmed ? 'confirmed' : null];
        }
        return [field, profile[field]];
    });
}
function calculateRequiredCompletion(items, labels) {
    const required = items.map(([field]) => labels[field]);
    const missing = items.filter(([, value]) => !hasValue(value)).map(([field]) => labels[field]);
    const completion = Math.round(((items.length - missing.length) / items.length) * 100);
    return {
        completion,
        missing,
        required,
        complete: missing.length === 0
    };
}
function hasValue(value) {
    if (typeof value === 'string') {
        return value.trim().length > 0;
    }
    if (typeof value === 'boolean') {
        return value;
    }
    if (Array.isArray(value)) {
        return value.length > 0;
    }
    if (value && typeof value === 'object') {
        return Object.keys(value).length > 0;
    }
    return value !== null && value !== undefined;
}
function hasListValue(value) {
    return Array.isArray(value) ? value.length > 0 : hasValue(value);
}
function hasObjectValue(value) {
    return Boolean(value && typeof value === 'object' && !Array.isArray(value) && Object.keys(value).length > 0);
}
//# sourceMappingURL=profile-completion.js.map