import uploadcareWidget from 'uploadcare-widget';

export const uploadcare = (component, callback) => { // callback with signature parameter `url`
  const uploadcarePanel = uploadcareWidget.openPanel(component.$refs['uploadcare-panel'], null, {
    tabs: ['file'],
  });

  // HACK to change the text of the Uploadcare dynamically we need to change the text here directly
  setTimeout(() => {
    const formElem = component.$refs['documentform'];
    if (formElem.getElementsByClassName('uploadcare--text_size_extra-large').length > 1) {
      formElem.getElementsByClassName('uploadcare--text_size_extra-large')[1].innerText = 'Ziehen Sie ein Dokument hier hinein';
      formElem.getElementsByClassName('uploadcare--tab__action-button')[0].innerText = 'Wählen Sie ein lokales Dokument';
    }
  }, 0);

  return uploadcarePanel.done(panelResult => {
    panelResult.done(fileInfo => {
      let urlWithFilename = fileInfo.cdnUrl + fileInfo.name;
      callback(urlWithFilename);
    });
  });
};
