import utils from "../../common/utils.mjs";
import common from "../common/issue.mjs";
import { ConfigurationError } from "@pipedream/platform";
import jira from "../../jira.app.mjs";
export default {
  key: "jira-add-comment-to-issue",
  name: "Add Comment To Issue",
  description: "Adds a new comment to an issue. [See the documentation](https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-comments/#api-rest-api-3-issue-issueidorkey-comment-post)",
  version: "0.1.14",
  annotations: {
    destructiveHint: false,
    openWorldHint: true,
    readOnlyHint: false,
  },
  type: "action",
  props: {
    jira,
    cloudId: {
      propDefinition: [
        jira,
        "cloudId",
      ],
    },
    issueIdOrKey: {
      propDefinition: [
        jira,
        "issueIdOrKey",
        (c) => ({
          cloudId: c.cloudId,
        }),
      ],
    },
    comment: {
      type: "string",
      label: "Comment",
      description: "The comment text",
      optional: true,
    },
    body: {
      type: "object",
      label: "Body",
      description: "The comment text in [Atlassian Document Format](https://developer.atlassian.com/cloud/jira/platform/apis/document/structure/), e.g. `{\"type\":\"doc\",\"version\":1,\"content\":[{\"content\":[{\"text\":\"This is a comment\",\"type\":\"text\"}],\"type\":\"paragraph\"}]}`. Will overwrite comment if both comment and body are provided",
      optional: true,
    },
    visibility: {
      type: "object",
      label: "Visibility",
      description: "The group or role to which this comment is visible, See `Visibility` section of [doc](https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-comments/#api-rest-api-3-issue-issueidorkey-comment-post)",
      optional: true,
    },
    properties: {
      propDefinition: [
        jira,
        "properties",
      ],
      description: "A list of comment properties.",
    },
    additionalProperties: {
      propDefinition: [
        jira,
        "additionalProperties",
      ],
    },
    expand: {
      propDefinition: [
        jira,
        "expand",
      ],
      description: "The Jira REST API uses resource expansion, which means that some parts of a resource are not returned unless specified in the request. This parameter accepts `renderedBody`, which returns the comment body rendered in HTML.",
    },
  },
  async run({ $ }) {
    if (!this.comment && !this.body) {
      throw new ConfigurationError("Either comment or body is required");
    }
    const visibility = utils.parseObject(this.visibility);
    const body = this.body
      ? utils.parseObject(this.body)
      : common.methods.atlassianDocumentFormat(this.comment);
    const additionalProperties = utils.parseObject(this.additionalProperties);
    let properties;
    try {
      properties = JSON.parse(this.properties);
    } catch ( err ) {
      
    }
    const response = await this.jira.addCommentToIssue({
      $,
      cloudId: this.cloudId,
      issueIdOrKey: this.issueIdOrKey,
      params: {
        expand: this.expand,
      },
      data: {
        body,
        visibility,
        properties,
        ...additionalProperties,
      },
    });
    $.export("$summary", `Comment has been added to the issue with ID(or key): ${this.issueIdOrKey}`);
    return response;
  },
};