class ModalMidias {
  constructor(callback, multiple = false) {
    this.callback = callback;
    this.multiple = multiple;
  }

  openModal() {
    const frame = wp.media({
      title: 'Mídia',
      button: {
        text: 'Selecionar'
      },
      multiple: this.multiple
    });

    frame.on('select', () => {
      const selection = frame.state().get('selection');
      if (this.multiple) {
        const attachments = selection.map(attachment => attachment.toJSON());
        console.log(attachments);
        this.callback(attachments);
      } else {
        const attachment = selection.first().toJSON();
        this.callback(attachment);
      }
    });

    frame.open();
  }

}

export default ModalMidias;