"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.FIELD_ACTION_DISABLE_REASON_TOOLTIPS = exports.FIELD_ACTIONS = void 0;
exports.buildFieldActionGroups = buildFieldActionGroups;
exports.getFieldActionDisableReason = getFieldActionDisableReason;
exports.getFieldActionDisableState = getFieldActionDisableState;
var _react = _interopRequireDefault(require("react"));
var _wixUiIconsCommon = require("@wix/wix-ui-icons-common");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/FieldActions/FieldActions.tsx";
const getReadOnlySchemaReason = perms => (perms == null ? void 0 : perms.schemaEditable) === false ? 'schemaNotEditable' : null;
const checkEditPerms = (col, perms) => {
  const blocked = getReadOnlySchemaReason(perms);
  if (blocked) {
    return blocked;
  }
  if (perms != null && perms.editDisabled) {
    return 'editDisabled';
  }
  if (col.isSystem) {
    return 'systemField';
  }
  return null;
};
const checkAddPerms = (col, perms) => {
  const blocked = getReadOnlySchemaReason(perms);
  if (blocked) {
    return blocked;
  }
  if (perms != null && perms.addDisabled) {
    return 'addDisabled';
  }
  if (col.isSystem) {
    return 'systemField';
  }
  return null;
};
const checkDeletePerms = (col, perms) => {
  const blocked = getReadOnlySchemaReason(perms);
  if (blocked) {
    return blocked;
  }
  if (perms != null && perms.deleteDisabled) {
    return 'deleteDisabled';
  }
  if (col.isSystem) {
    return 'systemField';
  }
  if (col.isPrimary) {
    return 'alreadyPrimary';
  }
  return null;
};
const FIELD_ACTIONS = exports.FIELD_ACTIONS = {
  editField: {
    label: 'Edit Field',
    icon: /*#__PURE__*/_react.default.createElement(_wixUiIconsCommon.EditSmall, {
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 119,
        columnNumber: 11
      }
    }),
    getDisableReason: checkEditPerms
  },
  makePrimary: {
    label: 'Use as Primary Field',
    icon: /*#__PURE__*/_react.default.createElement(_wixUiIconsCommon.FlagSmall, {
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 124,
        columnNumber: 11
      }
    }),
    getDisableReason: (col, perms) => {
      const blocked = getReadOnlySchemaReason(perms);
      if (blocked) {
        return blocked;
      }
      if (perms != null && perms.editDisabled) {
        return 'editDisabled';
      }
      if (col.isPrimary) {
        return 'alreadyPrimary';
      }
      if (col.isSystem) {
        return 'systemField';
      }
      if (col.cellType !== 'text') {
        return 'nonTextField';
      }
      return null;
    }
  },
  hideColumn: {
    label: 'Hide Column',
    icon: /*#__PURE__*/_react.default.createElement(_wixUiIconsCommon.HiddenSmall, {
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 147,
        columnNumber: 11
      }
    })
  },
  duplicateField: {
    label: 'Duplicate Field',
    icon: /*#__PURE__*/_react.default.createElement(_wixUiIconsCommon.DuplicateSmall, {
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 151,
        columnNumber: 11
      }
    }),
    getDisableReason: checkAddPerms
  },
  duplicateFieldWithContent: {
    label: 'Duplicate Field with Content',
    icon: /*#__PURE__*/_react.default.createElement(_wixUiIconsCommon.DuplicateSmall, {
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 156,
        columnNumber: 11
      }
    }),
    getDisableReason: checkAddPerms
  },
  sortAscending: {
    label: 'Sort Ascending',
    icon: /*#__PURE__*/_react.default.createElement(_wixUiIconsCommon.SortAscendingSmall, {
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 161,
        columnNumber: 11
      }
    })
  },
  sortDescending: {
    label: 'Sort Descending',
    icon: /*#__PURE__*/_react.default.createElement(_wixUiIconsCommon.SortDescendingSmall, {
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 165,
        columnNumber: 11
      }
    })
  },
  filter: {
    label: 'Filter',
    icon: /*#__PURE__*/_react.default.createElement(_wixUiIconsCommon.ContentFilterSmall, {
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 169,
        columnNumber: 11
      }
    })
  },
  delete: {
    label: 'Delete',
    icon: /*#__PURE__*/_react.default.createElement(_wixUiIconsCommon.DeleteSmall, {
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 173,
        columnNumber: 11
      }
    }),
    skin: 'destructive',
    getDisableReason: checkDeletePerms
  }
};
const FIELD_ACTION_DISABLE_REASON_TOOLTIPS = exports.FIELD_ACTION_DISABLE_REASON_TOOLTIPS = {
  schemaNotEditable: 'This schema is read-only.',
  addDisabled: 'Adding fields is disabled.',
  editDisabled: 'Editing fields is disabled.',
  deleteDisabled: 'Deleting fields is disabled.',
  alreadyPrimary: 'This is the primary field.',
  systemField: "System fields can't be modified.",
  nonTextField: 'Only text fields can be made primary.'
};
function getFieldActionDisableReason(action, column, permissions) {
  var _FIELD_ACTIONS$action, _FIELD_ACTIONS$action2;
  return ((_FIELD_ACTIONS$action = (_FIELD_ACTIONS$action2 = FIELD_ACTIONS[action]).getDisableReason) == null ? void 0 : _FIELD_ACTIONS$action.call(_FIELD_ACTIONS$action2, column, permissions)) ?? null;
}
function getFieldActionDisableState(action, column, permissions) {
  const reason = getFieldActionDisableReason(action, column, permissions);
  return {
    disabled: reason !== null,
    disabledTooltip: reason ? FIELD_ACTION_DISABLE_REASON_TOOLTIPS[reason] : undefined
  };
}
function buildFieldActionGroups(args) {
  const {
    column,
    permissions,
    handlers,
    loading,
    groups
  } = args;
  return groups.map(group => group.flatMap(key => {
    const handler = handlers[key];
    if (!handler) {
      return [];
    }
    const def = FIELD_ACTIONS[key];
    const disable = getFieldActionDisableState(key, column, permissions);
    return [{
      label: def.label,
      icon: def.icon,
      skin: def.skin,
      onClick: () => handler(column.id),
      loading: loading == null ? void 0 : loading[key],
      ...disable
    }];
  })).filter(g => g.length > 0);
}
//# sourceMappingURL=FieldActions.js.map