"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var linkify_exports = {};
__export(linkify_exports, {
  linkify: () => linkify
});
module.exports = __toCommonJS(linkify_exports);
var import_linkify = require("./../../_shared/link/linkify/linkify.js");
const getUrlMatches = (text) => import_linkify.linkify.match(text) || [];
const linkify = (contentState, { anchorTarget, relValue }) => {
  let lastKey = Object.keys(contentState.entityMap).length > 0 ? Math.max(...Object.keys(contentState.entityMap).map((key) => parseInt(key, 10))) + 1 : 0;
  return contentState.blocks.reduce(
    (state, block) => {
      const { text } = block;
      const linkEntries = getUrlMatches(text).filter(({ text: url, index: start, lastIndex: end }) => {
        const alreadyHasEntity = hasEntityInRange(block, start, end);
        const longEnough = url.length >= 6;
        return !alreadyHasEntity && longEnough;
      }).map(({ text: url, index: start, lastIndex: end }, idx) => {
        lastKey += idx;
        return createEntity(lastKey, url, start, end, anchorTarget, relValue);
      });
      return {
        blocks: [
          ...state.blocks,
          {
            ...block,
            entityRanges: [
              ...block.entityRanges,
              ...linkEntries.map(
                ({ entityRange }) => entityRange
              )
            ]
          }
        ],
        entityMap: {
          ...linkEntries.reduce(
            (entityMap, { mapEntry }) => ({
              ...entityMap,
              ...mapEntry
            }),
            state.entityMap
          )
        }
      };
    },
    { blocks: [], entityMap: { ...contentState.entityMap } }
  );
};
const hasEntityInRange = (block, start, end) => block.entityRanges.some(({ offset, length }) => start < offset + length && end >= offset);
const createEntity = (entityKey, url, start, end, anchorTarget, relValue) => {
  const entityRange = {
    offset: start,
    length: end - start,
    key: entityKey
  };
  const mapEntry = {
    [entityKey]: {
      type: "LINK",
      mutability: "MUTABLE",
      data: {
        url,
        target: anchorTarget,
        rel: relValue
      }
    }
  };
  return { entityRange, mapEntry };
};
//# sourceMappingURL=linkify.js.map
