function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
/**
 * 组装 PisellFloorMapLayout props（含 floor-plan 持久化 onSave）
 */
import React, { useMemo } from 'react';
import { wrapFloorMapOnSaveWithRemotePersist } from '@pisell/materials';
import { saveViewConfigToShopFloorPlan, toastFloorPlanSaveError } from "./topologyFloorPlanApi";
import { getTopologyRenderItemByKind } from "../topologyRenderItems";
import { TopologyOverviewHud } from "../TopologyOverviewHud";
import { PISELL_TOPOLOGY_FLOOR_PLAN_CODE } from "../topologyFloorPlanConstants";
import { TopologyFloorMapEditToggle } from "../components/TopologyFloorMapEditToggle";
export function useTopologyFloorMapMerged(params) {
  var baseFloorConfig = params.baseFloorConfig,
    dataSources = params.dataSources,
    devices = params.devices,
    mode = params.mode,
    mapEditInternal = params.mapEditInternal,
    setMapEditInternal = params.setMapEditInternal,
    floorMapLayoutRef = params.floorMapLayoutRef,
    forceEditFromProps = params.forceEditFromProps,
    resolvedFloorPlanId = params.resolvedFloorPlanId,
    floorPlanRemoteId = params.floorPlanRemoteId,
    effectiveFloorPlanCode = params.effectiveFloorPlanCode,
    floorPlanCanvasWidth = params.floorPlanCanvasWidth,
    floorPlanCanvasHeight = params.floorPlanCanvasHeight,
    floorPlanName = params.floorPlanName,
    floorPlanSort = params.floorPlanSort,
    floorPlanStatus = params.floorPlanStatus,
    onFloorPlanPersisted = params.onFloorPlanPersisted,
    putFloorPlan = params.putFloorPlan,
    prevOnSave = params.prevOnSave,
    overviewHud = params.overviewHud,
    setRemoteFloorPlanView = params.setRemoteFloorPlanView,
    className = params.className,
    style = params.style;
  return useMemo(function () {
    var devicesById = new Map(devices.map(function (d) {
      return [String(d.id), d];
    }));
    var persistTargetId = floorPlanRemoteId !== null && floorPlanRemoteId !== void 0 ? floorPlanRemoteId : resolvedFloorPlanId;
    var canPersist = persistTargetId != null && Number.isFinite(persistTargetId) && Math.floor(persistTargetId) === persistTargetId && persistTargetId > 0;
    var onSaveWithApi = wrapFloorMapOnSaveWithRemotePersist({
      canPersist: canPersist,
      persistTargetId: persistTargetId !== null && persistTargetId !== void 0 ? persistTargetId : 0,
      persist: function persist(id, config) {
        return saveViewConfigToShopFloorPlan(id, config, {
          canvasWidth: floorPlanCanvasWidth,
          canvasHeight: floorPlanCanvasHeight,
          code: effectiveFloorPlanCode,
          name: floorPlanName,
          sort: floorPlanSort,
          status: floorPlanStatus,
          putFloorPlan: putFloorPlan != null ? function (planId, body) {
            return putFloorPlan(planId, body);
          } : undefined
        });
      },
      setRemoteFloorPlanView: setRemoteFloorPlanView,
      onFloorPlanPersisted: onFloorPlanPersisted,
      prevOnSave: prevOnSave,
      onPersistError: toastFloorPlanSaveError
    });
    var namespace = "topology:".concat(effectiveFloorPlanCode || PISELL_TOPOLOGY_FLOOR_PLAN_CODE);
    var overlay = overviewHud === false ? undefined : function (args) {
      return typeof overviewHud === 'undefined' || overviewHud === undefined ? /*#__PURE__*/React.createElement(TopologyOverviewHud, _extends({}, args, {
        edges: baseFloorConfig.edges
      })) : overviewHud;
    };
    return {
      className: className,
      style: _objectSpread({
        width: '100%',
        height: '100%',
        minHeight: 0,
        flex: 1
      }, style),
      items: [],
      renderItem: function renderItem() {
        return null;
      },
      floorMapConfig: baseFloorConfig,
      dataSources: dataSources,
      mode: mode,
      itemUnit: 'pixel',
      /** Topology 需要连线渲染与编辑；其它通用画布场景（reservation 等）默认关闭 */
      enableEdges: true,
      toolbarShowSave: true,
      onSave: onSaveWithApi,
      renderItemByKind: getTopologyRenderItemByKind(devicesById),
      floorMapViewportOverlay: overlay,
      viewportOverlayPlacement: 'top-start',
      viewportPersistNamespace: namespace,
      controls: {
        showZoom: true,
        showFitBounds: true,
        showReset: true,
        showGridToggle: true,
        horizontalAlign: 'end'
      },
      onExitEdit: forceEditFromProps ? undefined : function () {
        setMapEditInternal(false);
      },
      viewportDockTrailing: /*#__PURE__*/React.createElement(TopologyFloorMapEditToggle, {
        mapEditInternal: mapEditInternal,
        setMapEditInternal: setMapEditInternal,
        floorMapLayoutRef: floorMapLayoutRef,
        forceEditMode: forceEditFromProps
      }),
      pan: {
        enabled: true
      },
      zoom: {
        autoFitOnMount: true,
        min: 0.25,
        max: 2
      }
    };
  }, [baseFloorConfig, dataSources, devices, mode, mapEditInternal, setMapEditInternal, floorMapLayoutRef, forceEditFromProps, resolvedFloorPlanId, floorPlanRemoteId, effectiveFloorPlanCode, floorPlanCanvasWidth, floorPlanCanvasHeight, floorPlanName, floorPlanSort, floorPlanStatus, onFloorPlanPersisted, putFloorPlan, prevOnSave, overviewHud, setRemoteFloorPlanView, className, style]);
}