(function(){
  'use strict';
  var el = window.wp.element;
  var htmlEntities = window.wp.htmlEntities;
  var i18n = window.wp.i18n;
  var registry = window.wc && window.wc.wcBlocksRegistry;
  var settings = window.wc && window.wc.wcSettings;

  if (!registry || !settings) {
    return; // Blocks not available
  }

  const getData = () => {
    const data = settings.getSetting('hkd_converse_data', null);
    if (!data) {
      throw new Error('Converse initialization data is not available');
    }
    return data;
  };

  const Description = () => {
    const data = getData();
    const text = (data && data.description) ? data.description : '';
    const content = htmlEntities.decodeEntities(text);
    return el.createElement('div', null, content);
  };

  registry.registerPaymentMethod({
    name: 'hkd_converse',
    label: el.createElement(function Logo(){
      var d = getData();
      return el.createElement('img', { src: d && d.logo_url, alt: d && d.title });
    }, null),
    ariaLabel: i18n.__('Converse Bank payment method', 'hk-payment-gateway-for-converse'),
    canMakePayment: function(){ return true; },
    content: el.createElement(Description, null),
    edit: el.createElement(Description, null),
    supports: { features: (function(){ var d = getData(); return (d && d.supports) ? d.supports : []; })() }
  });
})();
