"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.LayoutSwitch = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _mobxReactLite = require("mobx-react-lite");
var _Table = require("../Table");
var _Grid = require("../Grid");
var _InitialLoader = require("../InitialLoader");
var _LayoutSwitchButton = require("../LayoutSwitchButton");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/LayoutSwitch/LayoutSwitch.tsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const LazyEditableTable = /*#__PURE__*/_react.default.lazy(() => Promise.resolve().then(() => _interopRequireWildcard(require('../EditableTable'))).then(m => ({
  default: m.EditableTable
})));
function _LayoutSwitch({
  state,
  tableProps,
  gridProps,
  spreadsheetProps
}) {
  const {
    viewTypeState,
    initTask,
    toolbar
  } = state;
  const isLoading = initTask.status.isIdle || initTask.status.isLoading;

  // Recompute only when a prop flips between defined and undefined.
  const availableOptions = (0, _react.useMemo)(() => viewTypeState.options.filter(option => {
    if (option === 'table') {
      return !!tableProps;
    }
    if (option === 'grid') {
      return !!gridProps;
    }
    if (option === 'spreadsheet') {
      return !!spreadsheetProps;
    }
    return false;
  }),
  // eslint-disable-next-line react-hooks/exhaustive-deps
  [!!tableProps, !!gridProps, !!spreadsheetProps]);
  (0, _react.useEffect)(() => state.init(), []);

  // If the stored viewType is not among the available options (e.g. 'grid' was
  // persisted but gridProps is not provided), persist the correction so the next
  // load starts from a valid value. changeViewType also updates dataCapsule.
  (0, _react.useEffect)(() => {
    if (availableOptions.length > 0 && !availableOptions.includes(viewTypeState.viewType)) {
      viewTypeState.changeViewType(availableOptions[0]);
    }
  }, [availableOptions, viewTypeState.viewType]);
  if (!availableOptions.length) {
    console.error('[LayoutSwitch] At least one of tableProps, gridProps, or spreadsheetProps must be provided.');
    return null;
  }
  const layoutSwitchButton = /*#__PURE__*/_react.default.createElement(_LayoutSwitchButton.LayoutSwitchButton, {
    state: viewTypeState,
    options: availableOptions,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 74,
      columnNumber: 5
    }
  });
  if (isLoading) {
    return /*#__PURE__*/_react.default.createElement(_InitialLoader.InitialLoader, {
      dataHook: "initial-loader",
      state: toolbar,
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 78,
        columnNumber: 12
      }
    });
  }

  // If viewType is not yet available (loaded from storage but xProp absent),
  // fall back locally without waiting for the correction effect to fire.
  const activeView = availableOptions.includes(viewTypeState.viewType) ? viewTypeState.viewType : availableOptions[0];
  if (activeView === 'table') {
    return /*#__PURE__*/_react.default.createElement("div", {
      "data-hook": "layout-view-table",
      style: {
        display: 'contents'
      },
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 89,
        columnNumber: 7
      }
    }, /*#__PURE__*/_react.default.createElement(_Table.Table, (0, _extends2.default)({}, tableProps, {
      tableGridSwitchButton: layoutSwitchButton,
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 90,
        columnNumber: 9
      }
    })));
  }
  if (activeView === 'grid') {
    return /*#__PURE__*/_react.default.createElement("div", {
      "data-hook": "layout-view-grid",
      style: {
        display: 'contents'
      },
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 97,
        columnNumber: 7
      }
    }, /*#__PURE__*/_react.default.createElement(_Grid.Grid, (0, _extends2.default)({}, gridProps, {
      tableGridSwitchButton: layoutSwitchButton,
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 98,
        columnNumber: 9
      }
    })));
  }
  if (activeView === 'spreadsheet') {
    return /*#__PURE__*/_react.default.createElement("div", {
      "data-hook": "layout-view-spreadsheet",
      style: {
        display: 'contents'
      },
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 105,
        columnNumber: 7
      }
    }, /*#__PURE__*/_react.default.createElement(_react.Suspense, {
      fallback: /*#__PURE__*/_react.default.createElement(_InitialLoader.InitialLoader, {
        dataHook: "initial-loader",
        state: toolbar,
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 107,
          columnNumber: 21
        }
      }),
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 106,
        columnNumber: 9
      }
    }, /*#__PURE__*/_react.default.createElement(LazyEditableTable, (0, _extends2.default)({}, spreadsheetProps, {
      tableGridSwitchButton: layoutSwitchButton,
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 109,
        columnNumber: 11
      }
    }))));
  }
  return null;
}
const LayoutSwitch = exports.LayoutSwitch = (0, _mobxReactLite.observer)(_LayoutSwitch);
//# sourceMappingURL=LayoutSwitch.js.map