/* eslint-disable no-param-reassign */
import { GridTextColumnClass } from '../descope-grid-text-column/GridTextColumnClass';

export class GridCustomColumnClass extends GridTextColumnClass {
  _defaultRenderer(cell, _col, model) {
    const content = model.item[this.path] || '';

    // we get a list of elements that can be used to render the content
    // each element can have a "pattern" attribute which contains regex expression
    // we are going over the elements, and when finding an element which is pattern matches the data,
    // we are cloning this element, and injecting the data as its child
    const contentEle = Array.from(this.children).find((child) => {
      const pattern = child.getAttribute('data-pattern');
      if (!pattern) return true;

      const regEx = new RegExp(pattern);
      return regEx.test(content);
    });

    if (!contentEle) {
      this.__setTextContent(cell, model.item[this.path] || '');

      return;
    }

    const newEle = contentEle.cloneNode(true);
    newEle.textContent = content || '';
    cell.innerHTML = '';
    cell.append(newEle);
  }
}
