text: "use strict";
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.default = void 0;
  
  var _react = _interopRequireWildcard(require("react"));
  
  var _uuid = _interopRequireDefault(require("@sanity/uuid"));
  
  var _viewColumnIcon = _interopRequireDefault(require("part:@sanity/base/view-column-icon"));
  
  var _router = require("part:@sanity/base/router");
  
  var _parsePanesSegment = require("../utils/parsePanesSegment");
  
  var _IntentResolver = _interopRequireDefault(require("../components/IntentResolver"));
  
  var _DeskTool = _interopRequireDefault(require("./DeskTool"));
  
  var _constants = require("../constants");
  
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  
  function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
  
  function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  
  function _extends() { _extends = Object.assign || 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); }
  
  function toState(pathSegment) {
    return (0, _parsePanesSegment.parsePanesSegment)(decodeURIComponent(pathSegment));
  }
  
  function toPath(panes) {
    return (0, _parsePanesSegment.encodePanesSegment)(panes);
  }
  
  function legacyEditParamsToState(params) {
    try {
      return JSON.parse(decodeURIComponent(params));
    } catch (err) {
      // eslint-disable-next-line no-console
      console.warn('Failed to parse JSON parameters');
      return {};
    }
  }
  
  function legacyEditParamsToPath(params) {
    return JSON.stringify(params);
  }
  
  var state = {
    activePanes: []
  };
  
  function setActivePanes(panes) {
    state.activePanes = panes;
  }
  
  function DeskToolPaneStateSyncer(props) {
    var _useRouterState = (0, _router.useRouterState)(),
        intent = _useRouterState.intent,
        params = _useRouterState.params,
        payload = _useRouterState.payload;
  
    (0, _react.useEffect)(() => {
      // Set active panes to blank on mount and unmount
      setActivePanes([]);
      return () => setActivePanes([]);
    }, []);
    return intent ? /*#__PURE__*/_react.default.createElement(_IntentResolver.default, {
      intent: intent,
      params: params,
      payload: payload
    }) : /*#__PURE__*/_react.default.createElement(_DeskTool.default, _extends({}, props, {
      onPaneChange: setActivePanes
    }));
  } // eslint-disable-next-line complexity
  
  
  function getIntentState(intentName, params, currentState, payload) {
    var paneSegments = currentState && currentState.panes || [];
    var activePanes = state.activePanes || [];
    var editDocumentId = params.id || (0, _uuid.default)();
    var isTemplate = intentName === 'create' && params.template; // Loop through open panes and see if any of them can handle the intent
  
    for (var i = activePanes.length - 1; i >= 0; i--) {
      var pane = activePanes[i];
  
      if (pane.canHandleIntent && pane.canHandleIntent(intentName, params, {
        pane,
        index: i
      })) {
        var paneParams = isTemplate ? {
          template: params.template
        } : _constants.EMPTY_PARAMS;
        return {
          panes: paneSegments.slice(0, i).concat([[{
            id: editDocumentId,
            params: paneParams,
            payload
          }]])
        };
      }
    }
  
    return {
      intent: intentName,
      params,
      payload
    };
  }
  
  var _default = {
    router: (0, _router.route)('/', [// "Asynchronous intent resolving" route
    _router.route.intents('/intent'), // Legacy fallback route, will be redirected to new format
    (0, _router.route)('/edit/:type/:editDocumentId', [(0, _router.route)({
      path: '/:params',
      transform: {
        params: {
          toState: legacyEditParamsToState,
          toPath: legacyEditParamsToPath
        }
      }
    })]), // The regular path - when the intent can be resolved to a specific pane
    (0, _router.route)({
      path: '/:panes',
      // Legacy URLs, used to handle redirects
      children: [(0, _router.route)('/:action', (0, _router.route)('/:legacyEditDocumentId'))],
      transform: {
        panes: {
          toState,
          toPath
        }
      }
    })]),
  
    canHandleIntent(intentName, params) {
      return Boolean(intentName === 'edit' && params.id || intentName === 'create' && params.type || intentName === 'create' && params.template);
    },
  
    getIntentState,
    title: 'Desk',
    name: 'desk',
    icon: _viewColumnIcon.default,
    component: DeskToolPaneStateSyncer
  };
  exports.default = _default;