import axios from 'axios';
import { getEnv } from '../../../read-env-config';
/* ====== Enums ====== */
export var EmailType;
(function (EmailType) {
    EmailType["TRANSACTION_BASIC"] = "transaction-basic";
    EmailType["HRM_REMIND"] = "hrm-remind";
    EmailType["ORDER_SUCCESS"] = "order-success";
    EmailType["FREESTYLE_CAMPAIGN"] = "freestyle-campaign";
})(EmailType || (EmailType = {}));
export var NotificationType;
(function (NotificationType) {
    NotificationType["SYSTEM"] = "SYSTEM";
    NotificationType["TO_ME"] = "TO_ME";
})(NotificationType || (NotificationType = {}));
/* ====== Main Function ====== */
export async function PHXPushNotificationService(payload) {
    var _a, _b;
    const producerApi = getEnv('NEXT_PUBLIC_PRODUCER_API', process.env.NEXT_PUBLIC_PRODUCER_API);
    const { hostname } = payload;
    if (!producerApi) {
        const errorMessage = `${hostname}: PRODUCER_API is not configured`;
        console.debug(errorMessage);
        throw new Error(errorMessage);
    }
    try {
        const { data } = await axios.post(`${producerApi}/emit/inapp-noti`, payload, {
            headers: {
                hostname,
            },
        });
        return (data !== null && data !== void 0 ? data : {
            status: 'success',
            message: 'Notification pushed successfully',
        });
    }
    catch (error) {
        let errorMessage = '';
        if ((_b = (_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) {
            const message = error.response.data.message;
            errorMessage = Array.isArray(message) ? message.join(', ') : message;
        }
        else if (error === null || error === void 0 ? void 0 : error.message) {
            errorMessage = error.message;
        }
        const finalMessage = `Error pushing in-app notification: ${errorMessage}`;
        console.error(finalMessage);
        throw new Error(finalMessage || 'Failed to process notification');
    }
}
//# sourceMappingURL=pushNotificationService.js.map