<?php

/**
 * This file contains package_quiqqer_customer_ajax_backend_customer_editComment
 */

use QUI\ERP\Customer\Customers;

/**
 *
 * @return array
 */
QUI::getAjax()->registerFunction(
    'package_quiqqer_customer_ajax_backend_customer_editComment',
    function ($userId, $commentId, $source, $comment) {
        QUI\Permissions\Permission::checkPermission('quiqqer.customer.editComments');

        $User = QUI::getUsers()->get($userId);

        Customers::getInstance()->editComment(
            $User,
            $commentId,
            $source,
            $comment
        );

        $Comments = QUI\ERP\Comments::getCommentsByUser($User);

        if (!$Comments instanceof QUI\ERP\Comments) {
            return [];
        }

        return $Comments->toArray();
    },
    ['userId', 'commentId', 'source', 'comment'],
    'Permission::checkAdminUser'
);
