"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var selectors_exports = {};
__export(selectors_exports, {
  default: () => intersection,
  getActiveBlockDeviceOverrideValue: () => getActiveBlockDeviceOverrideValue,
  getActiveBlockType: () => getActiveBlockType,
  getActiveBlockValue: () => getActiveBlockValue,
  getActiveTypographyMark: () => getActiveTypographyMark,
  getBlocksInSelection: () => getBlocksInSelection,
  getInlinesInSelection: () => getInlinesInSelection,
  getSelection: () => getSelection
});
module.exports = __toCommonJS(selectors_exports);
var import_slate = require("slate");
var import_controls = require("@makeswift/controls");
var import_unhangRange = __toESM(require("./utils/unhangRange"));
var import_shallowEqual = __toESM(require("../utils/shallowEqual"));
var import_isNonNullable = require("../utils/isNonNullable");
var import_deepEqual = __toESM(require("../utils/deepEqual"));
var import_keys = __toESM(require("../utils/keys"));
function getSelection(editor) {
  if (editor.selection)
    return (0, import_unhangRange.default)(editor, editor.selection);
  return {
    anchor: import_slate.Editor.start(editor, []),
    focus: import_slate.Editor.end(editor, [])
  };
}
function getBlocksInSelection(editor) {
  return Array.from(
    import_slate.Editor.nodes(editor, {
      at: getSelection(editor),
      match: (node) => import_controls.Slate.isRootBlock(node)
    })
  ).filter((entry) => import_controls.Slate.isRootBlock(entry[0]));
}
function getInlinesInSelection(editor) {
  return Array.from(
    import_slate.Editor.nodes(editor, {
      at: getSelection(editor),
      match: (node) => import_controls.Slate.isInline(node)
    })
  ).filter((entry) => import_controls.Slate.isInline(entry[0]));
}
function getActiveBlockType(editor) {
  const rootBlocks = getBlocksInSelection(editor).map(([node]) => node.type);
  return rootBlocks.reduce(
    (a, b) => a === b ? b : null,
    rootBlocks.at(0) ?? null
  );
}
function getActiveBlockValue(editor, key) {
  const blocks = getBlocksInSelection(editor);
  const active = blocks.map(
    ([block]) => block[key] ?? null
  );
  return active.length === 0 ? null : active.reduce((a, b) => (0, import_shallowEqual.default)(a, b) ? b : null);
}
function getActiveBlockDeviceOverrideValue(editor, key, breakpoints, deviceId) {
  const active = [];
  const blocks = getBlocksInSelection(editor);
  blocks.forEach(([block]) => {
    const deviceOverride = (0, import_controls.findBreakpointOverride)(breakpoints, block[key], deviceId) || null;
    active.push(deviceOverride?.value ?? null);
  });
  return active.length === 0 ? null : active.reduce((a, b) => (0, import_shallowEqual.default)(a, b) ? b : null);
}
const concat = (a, b) => a.concat(b);
function intersection(a, b, isEqual = import_deepEqual.default) {
  const allKeys = [.../* @__PURE__ */ new Set([...(0, import_keys.default)(a), ...(0, import_keys.default)(b)])];
  return allKeys.reduce(
    (acc, k) => {
      if (isEqual(a[k], b[k]))
        acc[k] = a[k];
      else
        acc[k] = void 0;
      return acc;
    },
    {}
  );
}
const fuseTypographyMarks = (values, breakpoints) => {
  const devices = [
    ...new Set(
      values.filter(import_isNonNullable.isNonNullable).map(({ style }) => style.map(({ deviceId }) => deviceId)).reduce(concat, [])
    )
  ];
  return {
    id: values.map((v) => v && v.id).reduce((a, b) => a === b ? a : void 0) ?? void 0,
    style: devices.map(
      (deviceId) => values.map(
        (v) => v && (0, import_controls.findBreakpointOverride)(breakpoints, v.style, deviceId) || {
          deviceId,
          value: {}
        }
      ).reduce((acc, { value }) => {
        const a = intersection(acc.value, value);
        return {
          deviceId,
          value: a
        };
      })
    )
  };
};
function getActiveTypographyMark(editor, breakpoints) {
  const active = [];
  const textNodes = import_slate.Editor.nodes(editor, {
    at: getSelection(editor),
    match: (node) => import_slate.Text.isText(node)
  });
  for (const [node] of textNodes) {
    if (import_slate.Text.isText(node) && "typography" in node && node.typography != void 0) {
      active.push(node.typography);
    }
  }
  return active.length === 0 ? { style: [] } : fuseTypographyMarks(active, breakpoints);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getActiveBlockDeviceOverrideValue,
  getActiveBlockType,
  getActiveBlockValue,
  getActiveTypographyMark,
  getBlocksInSelection,
  getInlinesInSelection,
  getSelection
});
//# sourceMappingURL=selectors.js.map