import type { PlimiPlugin } from "../../core/plugins/plugin-types";
import { runCsvTools } from "./run";

export interface CsvToolsOptions {
  mode: "csv-to-json" | "json-to-csv";
  delimiter: "," | ";" | "\t";
  hasHeaderRow: boolean;
  prettyJson: boolean;
}

export const csvToolsPlugin: PlimiPlugin<CsvToolsOptions> = {
  manifest: {
    id: "csv-tools",
    name: "CSV <-> JSON Converter",
    description: "Convert CSV text to JSON objects, or convert JSON array of objects/arrays to CSV.",
    category: "developer",
    version: "1.0.0",
    tags: ["csv", "json", "converter", "parser", "format"],
    input: {
      type: "text",
      label: "Input Content",
      placeholder: "Paste CSV text or JSON array here...",
      multiline: true,
      rows: 10,
    },
    output: { type: "text" },
    example: "name,age,city\nJohn Doe,30,New York\nJane Smith,25,Los Angeles",
    offlineReady: true,
  },

  optionsSchema: {
    fields: [
      {
        type: "select",
        key: "mode",
        label: "Conversion Mode",
        defaultValue: "csv-to-json",
        options: [
          { label: "CSV to JSON", value: "csv-to-json" },
          { label: "JSON to CSV", value: "json-to-csv" },
        ],
      },
      {
        type: "select",
        key: "delimiter",
        label: "Delimiter / Separator",
        defaultValue: ",",
        options: [
          { label: "Comma (,)", value: "," },
          { label: "Semicolon (;)", value: ";" },
          { label: "Tab (\\t)", value: "\t" },
        ],
      },
      {
        type: "boolean",
        key: "hasHeaderRow",
        label: "First row is header (CSV -> JSON)",
        defaultValue: true,
      },
      {
        type: "boolean",
        key: "prettyJson",
        label: "Pretty Print JSON",
        defaultValue: true,
      },
    ],
  },

  capabilities: {
    cancelable: false,
    worker: false,
  },

  run: runCsvTools,
};
