/**
 * This file is part of the NocoBase (R) project.
 * Copyright (c) 2020-2024 NocoBase Co., Ltd.
 * Authors: NocoBase Team.
 *
 * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
 * For more information, please refer to: https://www.nocobase.com/agreement.
 */

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 layout_exports = {};
__export(layout_exports, {
  buildAutoLayout: () => buildAutoLayout,
  didItemRefsChange: () => didItemRefsChange,
  emitLayoutOp: () => emitLayoutOp,
  emitMoveOps: () => emitMoveOps,
  emitMovePopupTabOps: () => emitMovePopupTabOps,
  emitMoveTabOps: () => emitMoveTabOps,
  omitLayoutProps: () => omitLayoutProps
});
module.exports = __toCommonJS(layout_exports);
var import_lodash = __toESM(require("lodash"));
var import_placement = require("../placement");
function extractLayout(props) {
  if (!props || !("rows" in props) && !("sizes" in props) && !("rowOrder" in props)) {
    return null;
  }
  return {
    rows: props.rows || {},
    sizes: props.sizes || {},
    rowOrder: props.rowOrder || Object.keys(props.rows || {})
  };
}
function omitLayoutProps(props) {
  if (!props) {
    return props;
  }
  const next = import_lodash.default.omit(props, ["rows", "sizes", "rowOrder"]);
  return Object.keys(next).length ? next : void 0;
}
function resolveLayoutRefs(layout, childClientKeyRefs) {
  if (!layout) {
    return layout;
  }
  return {
    rows: Object.fromEntries(
      Object.entries(layout.rows || {}).map(([rowId, cells]) => [
        rowId,
        import_lodash.default.castArray(cells).map(
          (cell) => import_lodash.default.castArray(cell).map((itemUid) => childClientKeyRefs[itemUid] ?? itemUid)
        )
      ])
    ),
    sizes: layout.sizes || {},
    rowOrder: layout.rowOrder || []
  };
}
function hasRefValue(value) {
  if (Array.isArray(value)) {
    return value.some((item) => hasRefValue(item));
  }
  if (import_lodash.default.isPlainObject(value)) {
    if (typeof value.ref === "string") {
      return true;
    }
    return Object.values(value).some((item) => hasRefValue(item));
  }
  return false;
}
function buildAutoLayout(itemRefs) {
  if (!itemRefs.length) {
    return {
      rows: {},
      sizes: {},
      rowOrder: []
    };
  }
  return {
    rows: Object.fromEntries(itemRefs.map((itemRef, index) => [`autoRow${index + 1}`, [[itemRef]]])),
    sizes: Object.fromEntries(itemRefs.map((_2, index) => [`autoRow${index + 1}`, [24]])),
    rowOrder: itemRefs.map((_2, index) => `autoRow${index + 1}`)
  };
}
function didItemRefsChange(currentChildren, desiredRefs) {
  if (currentChildren.length !== desiredRefs.length) {
    return true;
  }
  return desiredRefs.some(
    (item, index) => {
      var _a;
      return typeof item.uidRef !== "string" || ((_a = currentChildren[index]) == null ? void 0 : _a.uid) !== item.uidRef;
    }
  );
}
function emitLayoutOp(ops, target, currentNode, desiredNode, childSync) {
  const explicitLayout = resolveLayoutRefs(extractLayout(desiredNode.props), childSync.childClientKeyRefs);
  const desiredLayout = explicitLayout || ((0, import_placement.isGridUse)((currentNode == null ? void 0 : currentNode.use) || desiredNode.use) && childSync.itemsSpecified ? buildAutoLayout(childSync.finalItemRefs.map((item) => item.uidRef)) : null);
  if (!desiredLayout) {
    return;
  }
  if (!hasRefValue(desiredLayout) && import_lodash.default.isEqual(desiredLayout, extractLayout(currentNode == null ? void 0 : currentNode.props))) {
    return;
  }
  ops.push({
    type: "setLayout",
    target,
    values: desiredLayout
  });
}
function emitMoveOps(ops, desiredRefs) {
  for (let index = desiredRefs.length - 2; index >= 0; index -= 1) {
    ops.push({
      type: "moveNode",
      values: {
        sourceUid: desiredRefs[index].uidRef,
        targetUid: desiredRefs[index + 1].uidRef,
        position: "before"
      }
    });
  }
}
function emitOrderedTabMoves(ops, desiredRefs, type) {
  for (let index = desiredRefs.length - 2; index >= 0; index -= 1) {
    ops.push({
      type,
      values: {
        sourceUid: desiredRefs[index].uidRef,
        targetUid: desiredRefs[index + 1].uidRef,
        position: "before"
      }
    });
  }
}
function emitMoveTabOps(ops, desiredRefs) {
  emitOrderedTabMoves(ops, desiredRefs, "moveTab");
}
function emitMovePopupTabOps(ops, desiredRefs) {
  emitOrderedTabMoves(ops, desiredRefs, "movePopupTab");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildAutoLayout,
  didItemRefsChange,
  emitLayoutOp,
  emitMoveOps,
  emitMovePopupTabOps,
  emitMoveTabOps,
  omitLayoutProps
});
