import lodash from "lodash";
import { HEADER_BLOCK } from "../consts.js";
const INLINE_HEADER = {
  ONE: "inline-header-one",
  TWO: "inline-header-two",
  THREE: "inline-header-three",
  FOUR: "inline-header-four",
  FIVE: "inline-header-five",
  SIX: "inline-header-six"
};
const INLINE_HEADERS = [INLINE_HEADER.THREE, INLINE_HEADER.TWO, INLINE_HEADER.ONE];
const INLINE_HEADER_TO_BLOCK = {
  [INLINE_HEADER.ONE]: HEADER_BLOCK.ONE,
  [INLINE_HEADER.TWO]: HEADER_BLOCK.TWO,
  [INLINE_HEADER.THREE]: HEADER_BLOCK.THREE,
  [INLINE_HEADER.FOUR]: HEADER_BLOCK.FOUR,
  [INLINE_HEADER.FIVE]: HEADER_BLOCK.FIVE,
  [INLINE_HEADER.SIX]: HEADER_BLOCK.SIX
};
const removeInlineHeaderRanges = (block) => {
  const inlineHeaderRanges = getInlineHeaderRanges(block.inlineStyleRanges || []);
  if (lodash.isEmpty(inlineHeaderRanges)) {
    return block;
  }
  return {
    ...block,
    type: getBlockType(block.type, block.text, inlineHeaderRanges),
    inlineStyleRanges: omitInlineHeaderRanges(block.inlineStyleRanges || [])
  };
};
const isInlineHeaderRange = (range) => lodash.includes(INLINE_HEADERS, range.style);
const omitInlineHeaderRanges = (ranges) => ranges.filter(lodash.negate(isInlineHeaderRange));
const getInlineHeaderRanges = (ranges) => ranges.filter(isInlineHeaderRange);
const getBlockType = (type, text, inlineHeaderRanges) => type === "unstyled" && shouldConvertToHeaderBlock(text, inlineHeaderRanges) ? getBlockHeaderType(inlineHeaderRanges) : type;
const shouldConvertToHeaderBlock = (text, inlineStyleRanges) => lodash.sortBy(inlineStyleRanges, "offset").map((range) => [range.offset, range.offset + range.length]).reduce((ranges, range) => {
  const lastRange = lodash.last(ranges);
  return lastRange && isOverlapping(lastRange, range) ? [...lodash.initial(ranges), mergeOverlappingRanges(lastRange, range)] : [...ranges, range];
}, []).reverse().reduce((text2, range) => `${text2.slice(0, range[0])}${text2.slice(range[1])}`, text).replace(/\s/g, "").length === 0;
const isInRange = (number, range) => range[0] <= number && number <= range[1];
const isOverlapping = (rangeA, rangeB) => rangeA && rangeB && (isInRange(rangeB[0], rangeA) || isInRange(rangeB[1], rangeA));
const mergeOverlappingRanges = (rangeA, rangeB) => [
  Math.min(rangeA[0], rangeB[0]),
  Math.max(rangeA[1], rangeB[1])
];
const getBlockHeaderType = (inlineHeaderRanges) => {
  const smallestInlineHeader = lodash.intersection(
    INLINE_HEADERS,
    inlineHeaderRanges.map((range) => range.style)
  )[0];
  return INLINE_HEADER_TO_BLOCK[smallestInlineHeader];
};
export {
  removeInlineHeaderRanges
};
//# sourceMappingURL=remove-inline-header-ranges.js.map
