<?php
/**
 * crisp_widget(array $user = []) — widget Crisp para el app
 * Incluir justo antes de </body>
 */
function crisp_widget(array $user = []): void {
    $crisp = defined('CRISP_WEBSITE_ID') ? CRISP_WEBSITE_ID : '';
    if (!$crisp) return; ?>
<!-- Crisp Support Widget -->
<script type="text/javascript">
window.$crisp=[];window.CRISP_WEBSITE_ID="<?=htmlspecialchars($crisp)?>";
(function(){var d=document;var s=d.createElement("script");s.src="https://client.crisp.chat/l.js";s.async=1;d.getElementsByTagName("head")[0].appendChild(s);})();
<?php if (!empty($user)): ?>
window.CRISP_READY_TRIGGER=function(){
  <?php if (!empty($user['email'])): ?>$crisp.push(["set","user:email","<?=addslashes($user['email'])?>"]);<?php endif; ?>
  <?php if (!empty($user['name'])): ?>$crisp.push(["set","user:nickname","<?=addslashes($user['name'])?>"]);<?php endif; ?>
  <?php if (!empty($user['plan'])): ?>$crisp.push(["set","session:segments",[["<?=addslashes($user['plan'])?>","cliente"]]]);<?php endif; ?>
};
<?php endif; ?>
</script>
<?php
}
