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);

// src/components/pisellFloorMapLayout/components/FloorMapEditTopBar.tsx
var FloorMapEditTopBar_exports = {};
__export(FloorMapEditTopBar_exports, {
  FloorMapEditTopBar: () => FloorMapEditTopBar,
  default: () => FloorMapEditTopBar_default
});
module.exports = __toCommonJS(FloorMapEditTopBar_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_antd = require("antd");
var import_HistoryIcons = require("./HistoryIcons");
var import_utils = require("@pisell/utils");
var import_useEngineContext = __toESM(require("../../../hooks/useEngineContext"));
var import_floorMapCanvasDisplay = require("../utils/floorMapCanvasDisplay");
var import_CanvasTabs = require("./CanvasTabs");
var import_FloorMapEditTopBar = require("./FloorMapEditTopBar.less");
var PREFIX = "pisell-floor-map-layout";
var I18N_TB = "pisell-floor-map-layout.edit-top-bar";
function detectIsMacLike() {
  if (typeof navigator === "undefined") return false;
  const p = navigator.platform || "";
  const ua = navigator.userAgent || "";
  return /Mac|iPhone|iPad|iPod/i.test(p) || /Macintosh/i.test(ua);
}
function FloorMapEditTopBar(props) {
  const {
    mode = "full",
    config,
    canvases,
    currentCanvasId,
    onTabChange,
    canEditTabs = false,
    onAddCanvas,
    onRemoveCanvas,
    onRenameCanvas,
    onSave,
    saving,
    saveError,
    layoutDirty,
    isFullscreen = false,
    extraActions,
    canvasTabsBarTrailing,
    hideCanvasTabs = false,
    onUndo,
    onRedo,
    canUndo = false,
    canRedo = false,
    onExitEdit,
    onCancelEdit,
    className,
    phoneLayout = false
  } = props;
  const engineCtx = (0, import_useEngineContext.default)();
  const engineLocale = (0, import_floorMapCanvasDisplay.getFloorMapDisplayLocale)(engineCtx);
  const topBarLabels = (0, import_react.useMemo)(() => {
    (0, import_floorMapCanvasDisplay.initFloorMapLayoutLocales)(engineLocale);
    return {
      aria: import_utils.locales.getText(`${I18N_TB}.aria`),
      unsaved: import_utils.locales.getText(`${I18N_TB}.unsaved`),
      save: import_utils.locales.getText(`${I18N_TB}.save`),
      saving: import_utils.locales.getText(`${I18N_TB}.saving`),
      undo: import_utils.locales.getText(`${I18N_TB}.undo`),
      redo: import_utils.locales.getText(`${I18N_TB}.redo`),
      cancelEdit: import_utils.locales.getText(`${I18N_TB}.cancel-edit`),
      finishEdit: import_utils.locales.getText(`${I18N_TB}.finish-edit`),
      cancelConfirmTitle: import_utils.locales.getText(`${I18N_TB}.cancel-confirm-title`),
      cancelConfirmContent: import_utils.locales.getText(
        `${I18N_TB}.cancel-confirm-content`
      ),
      cancelConfirmOk: import_utils.locales.getText(`${I18N_TB}.cancel-confirm-ok`),
      cancelConfirmCancel: import_utils.locales.getText(`${I18N_TB}.cancel-confirm-cancel`)
    };
  }, [engineLocale]);
  const showHistory = Boolean(onUndo || onRedo);
  const historyGroup = (0, import_react.useMemo)(() => {
    if (!showHistory) return null;
    const isMac = detectIsMacLike();
    const cmd = isMac ? "⌘" : "Ctrl";
    const undoTitle = `${topBarLabels.undo} (${cmd}+Z)`;
    const redoTitle = `${topBarLabels.redo} (${cmd}+Shift+Z)`;
    return /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: `${PREFIX}-edit-top-bar-history`,
        role: "group",
        "aria-label": `${topBarLabels.undo} / ${topBarLabels.redo}`
      },
      /* @__PURE__ */ import_react.default.createElement(
        import_antd.Button,
        {
          type: "text",
          size: "small",
          className: `${PREFIX}-edit-top-bar-history-btn`,
          icon: /* @__PURE__ */ import_react.default.createElement(import_HistoryIcons.UndoHistoryIcon, null),
          disabled: !canUndo,
          onClick: onUndo,
          "aria-label": undoTitle
        },
        topBarLabels.undo
      ),
      /* @__PURE__ */ import_react.default.createElement(
        import_antd.Button,
        {
          type: "text",
          size: "small",
          className: `${PREFIX}-edit-top-bar-history-btn`,
          icon: /* @__PURE__ */ import_react.default.createElement(import_HistoryIcons.RedoHistoryIcon, null),
          disabled: !canRedo,
          onClick: onRedo,
          "aria-label": redoTitle
        },
        topBarLabels.redo
      )
    );
  }, [showHistory, topBarLabels.undo, topBarLabels.redo, canUndo, canRedo, onUndo, onRedo]);
  const showSave = Boolean(onSave && config.canvasUi.canEdit) && !isFullscreen;
  const showExitGroup = Boolean(onExitEdit && config.canvasUi.canEdit);
  const dirtyBadge = layoutDirty && (showExitGroup || showSave) ? /* @__PURE__ */ import_react.default.createElement(
    "span",
    {
      className: `${PREFIX}-edit-top-bar-dirty-badge`,
      role: "status",
      "aria-live": "polite"
    },
    topBarLabels.unsaved
  ) : null;
  const [modalApi, modalContextHolder] = import_antd.Modal.useModal();
  const performCancel = (0, import_react.useCallback)(() => {
    if (onCancelEdit) onCancelEdit();
    else onExitEdit == null ? void 0 : onExitEdit();
  }, [onCancelEdit, onExitEdit]);
  const handleCancelClick = (0, import_react.useCallback)(() => {
    if (!layoutDirty) {
      performCancel();
      return;
    }
    modalApi.confirm({
      title: topBarLabels.cancelConfirmTitle,
      content: topBarLabels.cancelConfirmContent,
      okText: topBarLabels.cancelConfirmOk,
      cancelText: topBarLabels.cancelConfirmCancel,
      onOk: () => performCancel(),
      autoFocusButton: "cancel"
    });
  }, [
    layoutDirty,
    performCancel,
    modalApi,
    topBarLabels.cancelConfirmTitle,
    topBarLabels.cancelConfirmContent,
    topBarLabels.cancelConfirmOk,
    topBarLabels.cancelConfirmCancel
  ]);
  const hasTrailing = canvasTabsBarTrailing != null && canvasTabsBarTrailing !== false;
  const hasFullActions = showSave || showExitGroup || Boolean(saveError) || extraActions != null && extraActions !== false || hasTrailing || showHistory;
  const tabsHiddenClass = hideCanvasTabs ? `${PREFIX}-edit-top-bar--tabs-hidden` : void 0;
  if (mode === "compact") {
    const showCompactActions = hasTrailing || showHistory;
    const tabsInner = /* @__PURE__ */ import_react.default.createElement(
      import_CanvasTabs.CanvasTabs,
      {
        canvases,
        currentCanvasId,
        onTabChange,
        canEdit: canEditTabs,
        onAddCanvas,
        onRemoveCanvas,
        onRenameCanvas,
        className: `${PREFIX}-edit-top-bar-canvas-tabs`
      }
    );
    const actionsInner = /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, historyGroup, canvasTabsBarTrailing);
    if (phoneLayout) {
      return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, !hideCanvasTabs ? /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: (0, import_classnames.default)(
            `${PREFIX}-edit-top-bar`,
            `${PREFIX}-read-canvas-tabs-bar`,
            `${PREFIX}-edit-top-bar--compact`,
            `${PREFIX}-edit-top-bar--phone-edit-canvas-row`,
            className
          ),
          role: "toolbar",
          "aria-label": topBarLabels.aria
        },
        /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}-edit-top-bar-tabs` }, tabsInner)
      ) : null, showCompactActions ? /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: (0, import_classnames.default)(
            `${PREFIX}-edit-top-bar`,
            `${PREFIX}-edit-top-bar--compact`,
            `${PREFIX}-edit-top-bar--phone-actions-row`
          ),
          role: "toolbar"
        },
        /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}-edit-top-bar-actions` }, actionsInner)
      ) : null, modalContextHolder);
    }
    return /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: (0, import_classnames.default)(
          `${PREFIX}-edit-top-bar`,
          `${PREFIX}-read-canvas-tabs-bar`,
          `${PREFIX}-edit-top-bar--compact`,
          tabsHiddenClass,
          className
        ),
        role: "toolbar",
        "aria-label": topBarLabels.aria
      },
      !hideCanvasTabs ? /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}-edit-top-bar-tabs` }, tabsInner) : null,
      showCompactActions ? /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}-edit-top-bar-actions` }, actionsInner) : null,
      modalContextHolder
    );
  }
  if (hideCanvasTabs && !hasFullActions) {
    return null;
  }
  const fullTabsInner = /* @__PURE__ */ import_react.default.createElement(
    import_CanvasTabs.CanvasTabs,
    {
      canvases,
      currentCanvasId,
      onTabChange,
      canEdit: canEditTabs,
      onAddCanvas,
      onRemoveCanvas,
      onRenameCanvas,
      className: `${PREFIX}-edit-top-bar-canvas-tabs`
    }
  );
  const fullActionsInner = /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, saveError ? /* @__PURE__ */ import_react.default.createElement(
    "span",
    {
      className: `${PREFIX}-edit-top-bar-error`,
      title: typeof saveError === "string" ? saveError : saveError == null ? void 0 : saveError.message
    },
    typeof saveError === "string" ? saveError : saveError == null ? void 0 : saveError.message
  ) : null, dirtyBadge, historyGroup, canvasTabsBarTrailing, extraActions, showExitGroup ? /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}-edit-top-bar-save-group` }, /* @__PURE__ */ import_react.default.createElement(
    "button",
    {
      type: "button",
      className: `${PREFIX}-edit-top-bar-btn`,
      disabled: saving,
      onClick: handleCancelClick
    },
    topBarLabels.cancelEdit
  ), /* @__PURE__ */ import_react.default.createElement(
    "button",
    {
      type: "button",
      className: (0, import_classnames.default)(`${PREFIX}-edit-top-bar-btn`, "primary"),
      disabled: saving,
      onClick: () => {
        onSave == null ? void 0 : onSave();
        onExitEdit == null ? void 0 : onExitEdit();
      }
    },
    saving ? topBarLabels.saving : topBarLabels.finishEdit
  )) : showSave ? /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}-edit-top-bar-save-group` }, /* @__PURE__ */ import_react.default.createElement(
    "button",
    {
      type: "button",
      className: (0, import_classnames.default)(`${PREFIX}-edit-top-bar-btn`, "primary"),
      disabled: saving,
      onClick: onSave
    },
    saving ? topBarLabels.saving : topBarLabels.save
  )) : null);
  if (phoneLayout) {
    return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, !hideCanvasTabs ? /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: (0, import_classnames.default)(
          `${PREFIX}-edit-top-bar`,
          `${PREFIX}-read-canvas-tabs-bar`,
          `${PREFIX}-edit-top-bar--full-chrome`,
          `${PREFIX}-edit-top-bar--phone-edit-canvas-row`,
          className
        ),
        role: "toolbar",
        "aria-label": topBarLabels.aria
      },
      /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}-edit-top-bar-tabs` }, fullTabsInner)
    ) : null, hasFullActions ? /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: (0, import_classnames.default)(
          `${PREFIX}-edit-top-bar`,
          `${PREFIX}-edit-top-bar--full-chrome`,
          `${PREFIX}-edit-top-bar--phone-actions-row`
        ),
        role: "toolbar"
      },
      /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}-edit-top-bar-actions` }, fullActionsInner)
    ) : null, modalContextHolder);
  }
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)(
        `${PREFIX}-edit-top-bar`,
        `${PREFIX}-read-canvas-tabs-bar`,
        `${PREFIX}-edit-top-bar--full-chrome`,
        tabsHiddenClass,
        className
      ),
      role: "toolbar",
      "aria-label": topBarLabels.aria
    },
    !hideCanvasTabs ? /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}-edit-top-bar-tabs` }, fullTabsInner) : null,
    hasFullActions ? /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}-edit-top-bar-actions` }, fullActionsInner) : null,
    modalContextHolder
  );
}
var FloorMapEditTopBar_default = FloorMapEditTopBar;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  FloorMapEditTopBar
});
