import Immutable from "immutable";
import { msStringToDate, trigPrefixesAndBody } from "../../utils.js";
import { isUriRead } from "../../won-localstorage.js";

export function parseMessage(wonMessage, alreadyProcessed = false) {
  //seperating off header/@prefix-statements, so they can be folded in
  const { trigPrefixes, trigBody } = trigPrefixesAndBody(
    wonMessage.contentGraphTrig
  );

  let clauses = undefined;

  if (
    wonMessage.isProposeMessage() ||
    wonMessage.isAcceptMessage() ||
    wonMessage.isProposeToCancel()
  ) {
    if (wonMessage.isProposeMessage()) {
      clauses = wonMessage.getProposedMessages();
    } else if (wonMessage.isAcceptMessage()) {
      clauses = wonMessage.getAcceptedMessages();
    } else {
      clauses = wonMessage.getProposedToCancelMessages();
    }
  }

  const proposedMessages = wonMessage.getProposedMessages();
  const proposedToCancelMessages = wonMessage.getProposedToCancelMessages();
  const acceptedMessages = wonMessage.getAcceptedMessages();
  const rejectedMessages = wonMessage.getRejectsMessages();
  const retractedMessages = wonMessage.getRetractMessages();

  let parsedMessage = {
    belongsToUri: undefined,
    data: {
      uri: wonMessage.getMessageUri(),
      remoteUri: !wonMessage.isFromOwner()
        ? wonMessage.getRemoteMessageUri()
        : undefined,
      content: {
        text: wonMessage.getTextMessage(),
        matchScore: wonMessage.getMatchScore(),
      },
      references: {
        proposes:
          !proposedMessages || Array.isArray(proposedMessages)
            ? proposedMessages
            : [proposedMessages],
        proposesToCancel:
          !proposedToCancelMessages || Array.isArray(proposedToCancelMessages)
            ? proposedToCancelMessages
            : [proposedToCancelMessages],
        accepts:
          !acceptedMessages || Array.isArray(acceptedMessages)
            ? acceptedMessages
            : [acceptedMessages],
        rejects:
          !rejectedMessages || Array.isArray(rejectedMessages)
            ? rejectedMessages
            : [rejectedMessages],
        retracts:
          !retractedMessages || Array.isArray(retractedMessages)
            ? retractedMessages
            : [retractedMessages],
      },
      hasReferences: false, //will be determined by the hasReferences function
      hasContent: false, //will be determined by the hasContent function
      isParsable: false, //will be determined by the clause (hasReferences || hasContent) function
      isProposeMessage: wonMessage.isProposeMessage(),
      isAcceptMessage: wonMessage.isAcceptMessage(),
      isProposeToCancel: wonMessage.isProposeToCancel(),
      isRejectMessage: wonMessage.isRejectMessage(),
      isRetractMessage: wonMessage.isRetractMessage(),
      date: msStringToDate(wonMessage.getTimestamp()),
      outgoingMessage: wonMessage.isFromOwner(),
      messageType: wonMessage.getMessageType(),
      clauses: clauses,
      isRelevant: true,
      contentGraphTrig: {
        prefixes: trigPrefixes,
        body: trigBody,
      },
      contentGraphs: wonMessage.getContentGraphs(),
      contentGraphTrigRaw: wonMessage.contentGraphTrig,
      contentGraphTrigError: wonMessage.contentGraphTrigError,
      //Receive Status Flags
      unread:
        !wonMessage.isFromOwner() && !isUriRead(wonMessage.getMessageUri()),
      //Send Status Flags
      isReceivedByOwn: alreadyProcessed || !wonMessage.isFromOwner(), //if the message is not from the owner we know it has been received anyway
      isReceivedByRemote: alreadyProcessed || !wonMessage.isFromOwner(), //if the message is not from the owner we know it has been received anyway
      failedToSend: false,
    },
  };

  if (wonMessage.isFromOwner()) {
    parsedMessage.belongsToUri = wonMessage.getSender();
  } else {
    parsedMessage.belongsToUri = wonMessage.getReceiver();
  }

  parsedMessage.data.hasContent = hasContent(parsedMessage.data.content);
  parsedMessage.data.hasReferences = hasReferences(
    parsedMessage.data.references
  );
  parsedMessage.data.isParsable =
    parsedMessage.data.hasContent || parsedMessage.data.hasReferences;

  if (
    !parsedMessage.data.uri ||
    !parsedMessage.belongsToUri ||
    !parsedMessage.data.date
  ) {
    console.error(
      "Cant parse chat-message, data is an invalid message-object: ",
      wonMessage
    );
    return undefined;
  } else {
    return Immutable.fromJS(parsedMessage);
  }
}
function hasContent(content) {
  for (let prop in content) {
    if (content[prop] !== undefined && content[prop] != null) {
      return true;
    }
  }
  return false;
}

function hasReferences(refContent) {
  for (let prop in refContent) {
    if (refContent[prop] !== undefined && refContent[prop] != null) {
      return true;
    }
  }
  return false;
}

@@ -394,427 +394,8 @@
 );%0A%0A
-  let clauses = undefined;%0A%0A  if (%0A    wonMessage.isProposeMessage() %7C%7C%0A    wonMessage.isAcceptMessage() %7C%7C%0A    wonMessage.isProposeToCancel()%0A  ) %7B%0A    if (wonMessage.isProposeMessage()) %7B%0A      clauses = wonMessage.getProposedMessages();%0A    %7D else if (wonMessage.isAcceptMessage()) %7B%0A      clauses = wonMessage.getAcceptedMessages();%0A    %7D else %7B%0A      clauses = wonMessage.getProposedToCancelMessages();%0A    %7D%0A  %7D%0A%0A
   co
@@ -2520,32 +2520,8 @@
 (),%0A
-      clauses: clauses,%0A
     

remove clauses from parse-message