import { operatorProfiles } from "@rudderhq/db";
import { operatorProfileSettingsSchema, } from "@rudderhq/shared";
import { eq } from "drizzle-orm";
function normalizeField(value) {
    const trimmed = value?.trim();
    return trimmed ? trimmed : null;
}
function normalizeProfile(raw) {
    const parsed = operatorProfileSettingsSchema.safeParse(raw ?? {});
    if (!parsed.success) {
        return {
            nickname: "",
            moreAboutYou: "",
        };
    }
    return {
        nickname: normalizeField(parsed.data.nickname) ?? "",
        moreAboutYou: normalizeField(parsed.data.moreAboutYou) ?? "",
    };
}
export function operatorProfileService(db) {
    return {
        get: async (userId) => {
            const row = await db
                .select()
                .from(operatorProfiles)
                .where(eq(operatorProfiles.userId, userId))
                .then((rows) => rows[0] ?? null);
            return normalizeProfile({
                nickname: row?.nickname ?? "",
                moreAboutYou: row?.moreAboutYou ?? "",
            });
        },
        update: async (userId, patch) => {
            const current = await db
                .select()
                .from(operatorProfiles)
                .where(eq(operatorProfiles.userId, userId))
                .then((rows) => rows[0] ?? null);
            const next = normalizeProfile({
                nickname: patch.nickname ?? current?.nickname ?? "",
                moreAboutYou: patch.moreAboutYou ?? current?.moreAboutYou ?? "",
            });
            const now = new Date();
            await db
                .insert(operatorProfiles)
                .values({
                userId,
                nickname: normalizeField(next.nickname),
                moreAboutYou: normalizeField(next.moreAboutYou),
                createdAt: current?.createdAt ?? now,
                updatedAt: now,
            })
                .onConflictDoUpdate({
                target: operatorProfiles.userId,
                set: {
                    nickname: normalizeField(next.nickname),
                    moreAboutYou: normalizeField(next.moreAboutYou),
                    updatedAt: now,
                },
            });
            return next;
        },
    };
}
//# sourceMappingURL=operator-profile.js.map