"use strict";

exports.__esModule = true;
exports.CustomColumnsPanel = void 0;
exports._CustomColumnsPanel = _CustomColumnsPanel;
var _designSystem = require("@wix/design-system");
var _react = _interopRequireWildcard(require("react"));
var _react2 = require("@wix/bex-core/react");
var _mobxReactLite = require("mobx-react-lite");
var _ColumnsList = require("./ColumnsList");
var _CustomColumnsSearch = require("./CustomColumnsSearch");
var _CustomColumnsNoResults = require("./CustomColumnsNoResults");
var _useRafRender = require("../../hooks/useRafRender");
var _ScrollableContent = require("../ScrollableContent");
var _PatternsSidePanel = require("../SidePanel/PatternsSidePanel");
var _SchemaContext = require("../../providers/SchemaContext");
var _useSidePanelFieldActions = require("./useSidePanelFieldActions");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/CustomColumnsPanel/CustomColumnsPanel.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); }
function _CustomColumnsPanel({
  state
}) {
  var _schemaCtx$schema, _schemaCtx$schema2, _collectionDataExtens, _collectionDataExtens2, _collectionDataExtens3;
  const entered = (0, _useRafRender.useRafRender)();
  const {
    translate: t
  } = (0, _react2.useWixPatternsContainer)();
  const {
    dragAndDrop,
    collectionDataExtension
  } = state;
  const schemaCtx = (0, _SchemaContext.useSchemaContext)();
  const isSchema = !!(schemaCtx != null && schemaCtx.schema);
  const isEditable = !!collectionDataExtension || isSchema;
  const {
    fieldActionsByColumn,
    fieldActionsModal
  } = (0, _useSidePanelFieldActions.useSidePanelFieldActions)(isEditable ? state.columnsCollection.result.items : [], schemaCtx == null || (_schemaCtx$schema = schemaCtx.schema) == null ? void 0 : _schemaCtx$schema.fieldActions, schemaCtx == null || (_schemaCtx$schema2 = schemaCtx.schema) == null ? void 0 : _schemaCtx$schema2.refresh);
  (0, _react.useEffect)(() => state.init(), []);
  if (!entered) {
    return null;
  }
  const hasSearchResults = state.columnsCollection.result.items.length > 0;
  const hasSearchValue = state.searchValue.trim().length > 0;
  const totalColumns = state.orderedColumns.length;
  const isUsingDataExtension = !!collectionDataExtension;

  // Show search if they're using DEx OR if there are more than 5 columns
  const shouldShowSearch = isUsingDataExtension || totalColumns > 5;
  const titleKey = isEditable ? 'cairo.table.manage-fields.title' : 'cairo.table.custom-columns.title';
  const subtitleKey = isEditable ? dragAndDrop ? 'cairo.table.manage-fields.dragAndDrop.subtitle' : 'cairo.table.manage-fields.subtitle' : dragAndDrop ? 'cairo.table.custom-columns.dragAndDrop.subtitle' : 'cairo.table.custom-columns.subtitle';
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_PatternsSidePanel.PatternsSidePanel, {
    dataHook: "custom-columns-panel",
    width: "100%",
    onClose: () => state.onClose(),
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 63,
      columnNumber: 7
    }
  }, /*#__PURE__*/_react.default.createElement(_designSystem.SidePanel.Header, {
    title: t(titleKey),
    subtitle: t(subtitleKey),
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 68,
      columnNumber: 9
    }
  }, shouldShowSearch && /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
    padding: "0 SP3 SP2 SP3",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 70,
      columnNumber: 13
    }
  }, /*#__PURE__*/_react.default.createElement(_CustomColumnsSearch.CustomColumnsSearch, {
    state: state,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 71,
      columnNumber: 15
    }
  }))), /*#__PURE__*/_react.default.createElement(_ScrollableContent.ScrollableContent, {
    overflowY: true,
    dataHook: "custom-columns-panel-scroll-container",
    ref: e => {
      if (state.dragAndDrop) {
        state.dragAndDrop.listRect.element = e;
      }
    },
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 75,
      columnNumber: 9
    }
  }, hasSearchValue && !hasSearchResults ? /*#__PURE__*/_react.default.createElement(_CustomColumnsNoResults.CustomColumnsNoResults, {
    state: state,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 85,
      columnNumber: 13
    }
  }) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, (collectionDataExtension == null || (_collectionDataExtens = collectionDataExtension.dataExtension) == null ? void 0 : _collectionDataExtens.shouldDisplayArchive) && (collectionDataExtension == null || (_collectionDataExtens2 = collectionDataExtension.DataExtension) == null ? void 0 : _collectionDataExtens2.CustomColumnsArchivedItemsCTAButton) && /*#__PURE__*/_react.default.createElement(collectionDataExtension.DataExtension.CustomColumnsArchivedItemsCTAButton, {
    dataExtension: collectionDataExtension.dataExtension,
    onCloseSidePanel: () => state.onClose(),
    dataHook: "custom-columns",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 91,
      columnNumber: 19
    }
  }), /*#__PURE__*/_react.default.createElement(_ColumnsList.ColumnsList, {
    state: state,
    fieldActions: fieldActionsByColumn,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 97,
      columnNumber: 15
    }
  }))), (collectionDataExtension == null || (_collectionDataExtens3 = collectionDataExtension.DataExtension) == null ? void 0 : _collectionDataExtens3.CustomColumnsAddCustomFieldCTAButton) && /*#__PURE__*/_react.default.createElement(collectionDataExtension.DataExtension.CustomColumnsAddCustomFieldCTAButton, {
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 103,
      columnNumber: 11
    }
  }), (schemaCtx == null ? void 0 : schemaCtx.addField) && /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
    direction: "vertical",
    width: "100%",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 106,
      columnNumber: 11
    }
  }, /*#__PURE__*/_react.default.createElement(_designSystem.Divider, {
    direction: "horizontal",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 107,
      columnNumber: 13
    }
  }), /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
    padding: "SP4",
    width: "100%",
    boxSizing: "border-box",
    direction: "vertical",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 108,
      columnNumber: 13
    }
  }, /*#__PURE__*/_react.default.createElement(_designSystem.AddItem, {
    dataHook: "custom-columns-schema-add-field",
    onClick: schemaCtx.addField,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 114,
      columnNumber: 15
    }
  }, t('cairo.customFields.addCustomField.CTA'))))), fieldActionsModal);
}
const CustomColumnsPanel = exports.CustomColumnsPanel = (0, _mobxReactLite.observer)(_CustomColumnsPanel);
//# sourceMappingURL=CustomColumnsPanel.js.map