import React from "react";

import { Autocomplete, Chip, TextField } from "@mui/material";

export default function TagsInput({
  label = "Tags",
  value,
  onChange,
}: {
  label?: string;
  value: string[];
  onChange: (next: string[]) => void;
}) {
  return (
    <Autocomplete
      multiple
      freeSolo
      options={[]}
      value={value}
      onChange={(_, next) => {
        const cleaned = (next as string[])
          .map((t) => (t || "").trim())
          .filter(Boolean);
        onChange(Array.from(new Set(cleaned)));
      }}
      renderTags={(tagValue, getTagProps) =>
        tagValue.map((option: string, index: number) => (
          <Chip variant="outlined" label={option} {...getTagProps({ index })} key={option} size="small" />
        ))
      }
      renderInput={(params) => <TextField {...params} label={label} placeholder="Add a tag and press Enter" />}
    />
  );
}
