(function(){
  var i18n = window.CALLIOPE_SP_ADMIN_I18N || {};

  function t(key, fallback){
    return i18n[key] || fallback;
  }

  function getLink(){
    var el = document.getElementById('calliope_sp_link');
    return el ? (el.value || '').trim() : '';
  }
  document.addEventListener('DOMContentLoaded', function(){
    var btn = document.getElementById('calliope-sp-flipboard-share');
    if (!btn) return;
    btn.addEventListener('click', function(e){
      e.preventDefault();
      var href = getLink();
      if (!href) {
        alert(t('pasteLinkFirst', 'Paste a link to share first.'));
        return;
      }
      // Flipboard popup works best with URL only (titles/emojis can break).
      var u = 'https://share.flipboard.com/bookmarklet/popout?v=2&url=' + encodeURIComponent(href);
      window.open(u, 'flipshare', 'width=780,height=680');
    });
  });
})();
