import { Component, JSX } from "preact"
import { FoodExcludeTypes } from "./types";

export const FoodItemsContainer = (props: {
  children: JSX.Element | JSX.Element[] | null
  title: string,
}): JSX.Element => {
  return (
    <div>
      <h3>{props.title}</h3>
      <div class="list-group">
        {props.children}
      </div>
    </div>
  );
}

function shouldShowItem(contents, exclude: FoodExcludeTypes): boolean {

  for (let key of Object.keys(exclude)) {
    if (Object.keys(contents).includes(key)) {
      if (!exclude[key]) return false;
    }
  }
  return true;
}

type BaseFoodItemProps = {
  contents?: Partial<FoodExcludeTypes>,
  exclude?: FoodExcludeTypes,
}

const HideIfFoodExcluded = (props: BaseFoodItemProps & { children: JSX.Element }) => {
  if (!props.contents) {
    return props.children;
  } else if (shouldShowItem(props.contents, props.exclude)) {
    return props.children;
  } else {
    return <></>;
  }
}


type FoodItemProps = BaseFoodItemProps & {
  title: string,
};

export class FoodItem extends Component<FoodItemProps> {
  render() {
    return (
      <HideIfFoodExcluded {...this.props}>
        <li class="list-group-item">{this.props.title}</li>
      </HideIfFoodExcluded>
    )
  }
}

export class AdvancedFoodItem extends Component<FoodItemProps & { extra: string }> {
  render() {
    return (
      <HideIfFoodExcluded {...this.props}>
        <div class="list-group-item mb-1" role="listitem">
          <h5 class="mb-1">{this.props.title}</h5>
          <p class="mb-1">{this.props.extra}</p>
        </div>
      </HideIfFoodExcluded>
    )
  }
}