import { FetchHelper } from "../../../globals/FetchHelper";
export class PnChatService {
    _endpoint;
    _market;
    _language;
    _currentUrl;
    fetchHelper = new FetchHelper('PnChatService');
    _startChatPath = `/api/sfchat/start-chat`;
    _fetchChatMessagesPath = `/api/sfchat/get-conversation-entries`;
    _sendTextPath = `/api/sfchat/send-text-message`;
    _sendChoicePath = `/api/sfchat/send-choice-message`;
    _updateLastMessagePath = `/api/sfchat/update-last-message-id`;
    _hasNewMessagesPath = `/api/sfchat/has-new-messages`;
    constructor(endpoint, market, language) {
        this._endpoint = this.formatEndpoint(endpoint);
        this._market = market;
        this._language = language;
        this._currentUrl = window.location.href;
    }
    async startChat(chatId, itemId) {
        let model = {
            market: this._market,
            language: this._language,
            chatIdString: chatId,
            itemId: itemId,
            currentUrl: this._currentUrl
        };
        const fetchUrl = this._endpoint + this._startChatPath;
        const data = (await this.fetchHelper.fetchJson(fetchUrl, this.getRequestInit(model), false));
        return this.handleResponse(data, chatId);
    }
    async fetchMessages(chatId, prevMessageData) {
        if (!chatId) {
            return prevMessageData;
        }
        let model = {
            market: this._market,
            language: this._language,
            chatIdString: chatId
        };
        const fetchUrl = this._endpoint + this._fetchChatMessagesPath;
        const data = (await this.fetchHelper.fetchJson(fetchUrl, this.getRequestInit(model), false));
        if (!data) {
            return prevMessageData;
        }
        return this.handleResponse(data, prevMessageData);
    }
    async sendMessage(content, itemId, chatId, lastMessageId, messageType) {
        let newLatestMessageId;
        switch (messageType) {
            case 'Choice':
                newLatestMessageId = await this.sendChoiceMessage(content, itemId, chatId, lastMessageId);
                break;
            default:
                newLatestMessageId = await this.sendTextMessage(content, itemId, chatId, lastMessageId);
                break;
        }
        if (!newLatestMessageId) {
            return lastMessageId;
        }
        if (newLatestMessageId.includes(",")) {
            newLatestMessageId = newLatestMessageId.substring(newLatestMessageId.lastIndexOf(",") + 1);
        }
        return newLatestMessageId;
    }
    async updateLastMessageId(chatId, lastMessageId) {
        let model = {
            chatIdString: chatId,
            latestMessageIdString: lastMessageId
        };
        const fetchUrl = this._endpoint + this._updateLastMessagePath;
        await this.fetchHelper.fetchJson(fetchUrl, this.getRequestInit(model), false);
    }
    async hasNewMessages(itemId) {
        let model = {
            language: this._language,
            market: this._market,
            itemId: itemId,
            chatIdString: "",
            currentUrl: this._currentUrl
        };
        const fetchUrl = this._endpoint + this._hasNewMessagesPath;
        let data = await this.fetchHelper.fetchJson(fetchUrl, this.getRequestInit(model), false);
        if (!data) {
            return false;
        }
        return JSON.parse(data);
    }
    async sendTextMessage(content, itemId, chatId, defaultReturn) {
        if (!chatId || !content) {
            return defaultReturn;
        }
        let model = {
            market: this._market,
            language: this._language,
            chatIdString: chatId,
            content: content,
            itemId: itemId,
            currentUrl: this._currentUrl
        };
        const fetchUrl = this._endpoint + this._sendTextPath;
        const data = (await this.fetchHelper.fetchJson(fetchUrl, this.getRequestInit(model), false));
        return this.handleResponse(data, defaultReturn)?.id ?? defaultReturn;
    }
    async sendChoiceMessage(content, itemId, chatId, defaultReturn) {
        if (!chatId || !content) {
            return defaultReturn;
        }
        let model = {
            market: this._market,
            language: this._language,
            chatIdString: chatId,
            content: content,
            itemId: itemId,
            currentUrl: this._currentUrl
        };
        const fetchUrl = this._endpoint + this._sendChoicePath;
        const data = (await this.fetchHelper.fetchJson(fetchUrl, this.getRequestInit(model), false));
        return this.handleResponse(data, defaultReturn)?.id ?? defaultReturn;
    }
    formatEndpoint(endpoint) {
        return endpoint.lastIndexOf('/') === endpoint.length - 1 ? endpoint.substring(0, endpoint.length - 1) : endpoint;
    }
    handleResponse(data, defaultReturn, checkForArray = false) {
        if (!data) {
            return defaultReturn;
        }
        let parsedData = JSON.parse(data);
        if (!parsedData || checkForArray && !Array.isArray(parsedData)) {
            return defaultReturn;
        }
        return parsedData;
    }
    getRequestInit(model, method = 'POST', mode = 'cors', contentTypes = 'application/json') {
        return {
            mode: mode,
            method: method,
            headers: {
                'Content-Type': contentTypes,
                'CustomValidateAntiForgeryToken': "01001101010001010100111101010111"
            },
            body: JSON.stringify(model)
        };
    }
    GetLastMessageId(messages, lastMessageId) {
        return messages?.at(-1)?.messageId ?? lastMessageId;
    }
}
//# sourceMappingURL=pn-chat.service.js.map
