import todoist from "../../todoist.app.mjs";
import converter from "json-2-csv";
import { getFileStream } from "@pipedream/platform";
export default {
  key: "todoist-import-tasks",
  name: "Import Tasks",
  description: "Import tasks into a selected project. [See Docs](https://developer.todoist.com/sync/v9/#add-an-item)",
  version: "0.1.2",
  annotations: {
    destructiveHint: true,
    openWorldHint: true,
    readOnlyHint: false,
  },
  type: "action",
  props: {
    todoist,
    path: {
      propDefinition: [
        todoist,
        "path",
      ],
    },
    project: {
      propDefinition: [
        todoist,
        "project",
      ],
      description: "Project to import tasks into",
    },
    syncDir: {
      type: "dir",
      accessMode: "read",
      sync: true,
      optional: true,
    },
  },
  methods: {
    exportSummary($, tasks) {
      $.export("$summary", `Successfully imported ${tasks.length} task${tasks.length === 1
        ? ""
        : "s"} from "${this.path}"`);
    },
  },
  async run ({ $ }) {
    const {
      path,
      project,
    } = this;
    const stream = await getFileStream(path);
    const chunks = [];
    for await (const chunk of stream) {
      chunks.push(chunk);
    }
    const fileContents = Buffer.concat(chunks).toString();
    const tasks = converter.csv2json(fileContents);
    
    const data = tasks.map((task) => ({
      content: task.content,
      description: task.description,
      project_id: project,
      due: task.due,
      priority: task.priority,
      child_order: task.order,
      labels: task.label_ids,
    }));
    const {
      responses: createResponses,
      tempIds,
    } = await this.todoist.createTasks({
      $,
      data,
    });
    const childTasks = tasks.filter((task) => task.parent_id);
    if (childTasks.length === 0) {
      this.exportSummary($, tasks);
      return {
        createResponses,
      };
    }
    
    
    
    const tempIdMapping = createResponses.reduce((mapping, response) => {
      mapping = {
        ...mapping,
        ...response.temp_id_mapping,
      };
      return mapping;
    }, {});
    
    const idMapping = tasks.reduce((mapping, task, index) => {
      mapping[task.id] = tempIdMapping[tempIds[index]];
      return mapping;
    }, {});
    const moveData = childTasks.map((task) => ({
      id: idMapping[task.id],
      parent_id: idMapping[task.parent_id],
    }));
    const { responses: moveResponses } = await this.todoist.moveTasks({
      $,
      data: moveData,
    });
    this.exportSummary($, tasks);
    return {
      createResponses,
      moveResponses,
    };
  },
};