/*
 * Copyright (c) 2023-2025 Datalayer, Inc.
 * Distributed under the terms of the Modified BSD License.
 */
import { asContactIAMProvider, } from './ContactIAMProvider.js';
import { asContactEvent } from './ContactEvent.js';
import { asDisplayName, namesAsInitials } from '../utils/index.js';
export class Contact {
    id;
    handle;
    firstName;
    lastName;
    avatarUrl;
    initials;
    displayName;
    text;
    jobTitle;
    email;
    emailPersonal;
    countryCode;
    affiliation;
    affiliationUrl;
    affiliationLinkedinUrl;
    affiliationSecondary;
    affiliationTertiary;
    notes;
    tags;
    events = new Array();
    iamProviders = new Array();
    creationDate;
    lastUpdateDate;
    emailEnrichJson;
    linkedinEnrichJson;
    unsubscribedFromOutbounds;
    linkedUserUid;
    constructor(c) {
        this.id = c.uid;
        this.handle = c.handle;
        this.firstName = c.firstName ?? '';
        this.lastName = c.lastName ?? '';
        this.jobTitle = c.jobTitle ?? '';
        this.email = c.email ?? '';
        this.emailPersonal = c.emailPersonal ?? '';
        this.countryCode = c.countryCode ?? '';
        this.initials = namesAsInitials(this.firstName, this.lastName);
        const displayName = asDisplayName(this.firstName, this.lastName);
        this.displayName = displayName || '❓';
        this.text = this.displayName;
        this.avatarUrl = c.avatarUrl ?? '';
        this.affiliation = c.affiliation ?? '';
        this.affiliationUrl = c.affiliationUrl ?? '';
        this.affiliationLinkedinUrl = c.affiliationLinkedinUrl ?? '';
        this.affiliationSecondary = c.affiliationSecondary ?? '';
        this.affiliationTertiary = c.affiliationTertiary ?? '';
        this.notes = c.notes ?? '';
        this.tags = c.tags ?? [];
        let iamProviders = c.iamProviders ?? [];
        if (!Array.isArray(iamProviders)) {
            iamProviders = [iamProviders];
        }
        this.iamProviders = iamProviders.map(iamProvider => asContactIAMProvider(iamProvider));
        let events = c.events ?? [];
        if (!Array.isArray(events)) {
            events = [events];
        }
        this.events = events.map(event => asContactEvent(event));
        this.events.sort((x, y) => (x.eventDate < y.eventDate ? 1 : -1));
        this.creationDate = new Date(c.creationDate);
        this.lastUpdateDate = new Date(c.lastUpdateDate);
        this.emailEnrichJson = c.emailEnrichJson ?? '{}';
        this.linkedinEnrichJson = c.linkedinEnrichJson ?? '{}';
        this.unsubscribedFromOutbounds = c.unsubscribedFromOutbounds ?? false;
        this.linkedUserUid = c.linkedUserUid ?? '';
    }
    setTags(tags) {
        this.tags = tags;
    }
    addEvent(event) {
        this.events.push(event);
    }
}
/**
 * Convert the raw contact object to {@link IContact}.
 *
 * @param c Raw contact object
 * @returns Contact
 */
export function asContact(c) {
    return new Contact(c);
}
