{"version":3,"file":"util.format-block.cjs","sources":["../../../../src/rules/limit-multi-line-comments/util.format-block.ts"],"sourcesContent":["import type { Context } from \"../../typings.context.js\";\nimport { isAnotherWrapPointComing } from \"../../utils/is-another-wrap-point-coming.js\";\nimport { isPunctuation } from \"../../utils/is-punctuation.js\";\n\nimport type { MultilineBlock } from \"./typings.block.js\";\nimport { MULTILINE_BOILERPLATE_SIZE } from \"./util.boilerplate-size.js\";\n\n/**\n * takes a fixable block and transform it into a singular string which\n * represents the fixed format of the block.\n */\nexport function formatBlock(fixable: MultilineBlock, context: Context): string {\n  const lineStartSize =\n    context.whitespace.size +\n    MULTILINE_BOILERPLATE_SIZE +\n    (fixable.lineOffsets[0]?.size ?? 0);\n  const words = fixable.value.trim().split(\" \");\n\n  const newValue = words.reduce(\n    (acc, curr, index) => {\n      const lengthIfAdded = acc.currentLineLength + curr.length + 1; // + 1 to act as a final space, i.e. \" \"\n\n      // We can safely split to a new line in case we are reaching and\n      // overflowing line AND if there is at least one word on the current line.\n      const splitToNewline =\n        lengthIfAdded > context.maxLength &&\n        acc.currentLineLength !== lineStartSize;\n\n      const previousWord = words[index - 1];\n\n      const splitEarly =\n        context.logicalWrap &&\n        acc.currentLineLength >= context.maxLength / 2 &&\n        previousWord &&\n        previousWord.at(-1) &&\n        isPunctuation(previousWord.at(-1)) &&\n        previousWord.length > 1 &&\n        !isAnotherWrapPointComing(\n          acc.currentLineLength,\n          context.maxLength,\n          words.slice(index),\n        );\n\n      if (splitToNewline || splitEarly) {\n        const nextLine = `${context.whitespace.string} *${\n          fixable.lineOffsets[\n            Math.min(acc.currentLineIndex + 1, fixable.lineOffsets.length - 1)\n          ]?.string ?? \"\"\n        }${curr} `;\n\n        return {\n          value: `${acc.value.trimEnd()}\\n${nextLine}`,\n          currentLineLength: nextLine.length,\n          currentLineIndex: acc.currentLineIndex + 1,\n        };\n      } else {\n        return {\n          value: `${acc.value}${curr} `,\n          currentLineLength: lengthIfAdded,\n          currentLineIndex: acc.currentLineIndex,\n        };\n      }\n    },\n    {\n      value: `${context.whitespace.string} *${\n        fixable.lineOffsets[0]?.string ?? \"\"\n      }`,\n      currentLineLength: lineStartSize,\n      currentLineIndex: 0,\n    },\n  );\n\n  return newValue.value.trimEnd();\n}\n"],"names":["MULTILINE_BOILERPLATE_SIZE","isPunctuation","isAnotherWrapPointComing"],"mappings":";;;;;;AAWgB,SAAA,WAAA,CAAY,SAAyB,OAA0B,EAAA;AAC7E,EAAM,MAAA,aAAA,GACJ,QAAQ,UAAW,CAAA,IAAA,GACnBA,mDACC,OAAQ,CAAA,WAAA,CAAY,CAAC,CAAA,EAAG,IAAQ,IAAA,CAAA,CAAA,CAAA;AACnC,EAAA,MAAM,QAAQ,OAAQ,CAAA,KAAA,CAAM,IAAK,EAAA,CAAE,MAAM,GAAG,CAAA,CAAA;AAE5C,EAAA,MAAM,WAAW,KAAM,CAAA,MAAA;AAAA,IACrB,CAAC,GAAK,EAAA,IAAA,EAAM,KAAU,KAAA;AACpB,MAAA,MAAM,aAAgB,GAAA,GAAA,CAAI,iBAAoB,GAAA,IAAA,CAAK,MAAS,GAAA,CAAA,CAAA;AAI5D,MAAA,MAAM,cACJ,GAAA,aAAA,GAAgB,OAAQ,CAAA,SAAA,IACxB,IAAI,iBAAsB,KAAA,aAAA,CAAA;AAE5B,MAAM,MAAA,YAAA,GAAe,KAAM,CAAA,KAAA,GAAQ,CAAC,CAAA,CAAA;AAEpC,MAAM,MAAA,UAAA,GACJ,QAAQ,WACR,IAAA,GAAA,CAAI,qBAAqB,OAAQ,CAAA,SAAA,GAAY,CAC7C,IAAA,YAAA,IACA,YAAa,CAAA,EAAA,CAAG,EAAE,CAClB,IAAAC,2BAAA,CAAc,aAAa,EAAG,CAAA,CAAA,CAAE,CAAC,CACjC,IAAA,YAAA,CAAa,MAAS,GAAA,CAAA,IACtB,CAACC,iDAAA;AAAA,QACC,GAAI,CAAA,iBAAA;AAAA,QACJ,OAAQ,CAAA,SAAA;AAAA,QACR,KAAA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