(function () {
  const config = {
    elements: [
      {
        tagName: 'link',
        attrs: {
          rel: 'stylesheet',
          href: 'css/styles.css',
        },
      },
      {
        tagName: 'script',
        attrs: {
          src: 'js/scripts.js',
        },
      },
    ],
    source: 'https://source.wowmi.us/companies/c2financial/chatbot/',
  };
  
  const buildElements = ({ elements, source }) => {
    elements.forEach(({ tagName, attrs }) => {
      const el = document.createElement(tagName);
      
      // set attributes
      Object.entries(attrs).forEach(([key, value]) => {
        switch (true) {
          case key === 'href' || key === 'src':
            el.setAttribute(key, `${source}${value}`);
            break;
          
          default:
            el.setAttribute(key, value);
            break;
        }
      });
      
      document.body.append(el);
    });
  };
  
  const init = (config) => {
    buildElements(config);
  };
  
  document.addEventListener('DOMContentLoaded', function () {
    init(config);
  });
})();
