import { ColDef } from 'ag-grid-community';
import * as breeze from 'breeze-client';

export class ButtonsColDefAction<T extends breeze.Entity> {
  public icon: string | ((data: T) => string);
  public title: string | ((data: T) => string);
  public uiTheme: string | ((data: T) => string);
  public size: string | ((data: T) => string) = 'nm';
  public type: string | ((data: T) => string) = 'tool';
  public disabled: boolean | ((data: T) => boolean);
  public hidden: boolean | ((data: T) => boolean);
  public viewModel: any;

  public clicked: (data: T) => Promise<void>;

  constructor(init: Partial<ButtonsColDefAction<T>>) {
    Object.assign(this, init);
  }

  public static get uiBtnProps(): string[] {
    return ['title', 'uiTheme', 'size', 'type', 'active', 'disabled'];
  }
}

export function getButtonsColDef<T extends breeze.Entity>(buttons: ButtonsColDefAction<T>[] = []): ColDef {
  return {
    headerName: "",
    field: "buttons",
    maxWidth: 5 + 40 * buttons.length,
    minWidth: 5 + 40 * buttons.length,
    cellRenderer: "buttonRenderer",
    cellRendererParams: {
      buttons,
    },
    suppressColumnsToolPanel: true,
    suppressMenu: true,
    sortable: false,
    resizable: false,
    filter: false,
    lockPosition: "right",
    pinned: "right"
  };
}