import { masterCellBuilder } from "@applicaster/zapp-react-native-ui-components/Components/MasterCell";
import { containerStyleResolver } from "./containerStyles";
import { viewTreeResolver } from "./viewTrees";
// @ts-ignore
import { labelBuilder } from "./Label";

/**
 * Find matching view tree for the component type,
 * inject styles into the view tree, and returns a MasterCell.
 * The MasterCell accepts an item (the entry data) and a state (default/focused).
 * @param {object} configuration "styles" object from UI builder: grid / horizontal_list etc.
 * @returns {function} renderer, an instance of MasterCell.
 */
function defaultCellRendererFactory({ component, styles, cellOptions }) {
  if (!component || !component.component_type) {
    throw new Error("Missing component or component type");
  }

  // Important! Not every renderer has to be a master cell.
  // The label, for example, is just a function returning a react component.
  // e.g.: <MyCellRenderer item={item} state="focused" />
  switch (component.component_type) {
    case "header":
      return labelBuilder(styles);
    default:
      return masterCellBuilder({
        containerStyle: containerStyleResolver(component),
        cellConfiguration: viewTreeResolver(component)(styles),
        cellOptions,
      });
  }
}

const DefaultCellRenderer = defaultCellRendererFactory;

export default DefaultCellRenderer;
