import _defineProperty from '@babel/runtime/helpers/defineProperty';
import i18next from 'i18next';
import { toLabelAndValue, isNotEmpty } from '@bigbinary/neeto-cist';
import { DEFAULT_PAGE_SIZE, DEFAULT_PAGE_INDEX, SINGULAR } from '@bigbinary/neeto-commons-frontend/constants';
import { taxonomies } from '@bigbinary/neeto-commons-frontend/initializers';
import { getQueryParams, buildUrl, hyphenize } from '@bigbinary/neeto-commons-frontend/utils';
import MoreDropdown from '@bigbinary/neeto-molecules/MoreDropdown';
import Avatar from '@bigbinary/neetoui/Avatar';
import Dropdown from '@bigbinary/neetoui/Dropdown';
import Typography from '@bigbinary/neetoui/Typography';
import { clamp, curry, filter, identical, mergeLeft, assoc, not } from 'ramda';
import { Trans } from 'react-i18next';
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';

var _getQueryParams = getQueryParams(),
  page = _getQueryParams.page,
  pageSize = _getQueryParams.pageSize;
var DEFAULT_RADIX = 10;
var DEFAULT_IS_OPEN_VALUES = {
  menuBar: false,
  manageMember: false,
  alert: false
};
var DEFAULT_FILTER_VALUES = {
  page: clamp(1, Infinity, parseInt(page)) || DEFAULT_PAGE_INDEX,
  pageSize: clamp(10, 100, parseInt(pageSize)) || DEFAULT_PAGE_SIZE,
  sortBy: "",
  orderBy: "",
  neetoFilters: []
};
var DEFAULT_SELECTED_ROWS = {
  active: [],
  emails: [],
  keys: [],
  displayNames: []
};

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), true).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; }
var MenuItem = Dropdown.MenuItem;
var renderNameColumn = function renderNameColumn(_ref) {
  var config = _ref.config,
    handleUpdateRole = _ref.handleUpdateRole,
    handleUpdateStatus = _ref.handleUpdateStatus,
    tableData = _ref.tableData;
  var name = tableData.displayName,
    imageUrl = tableData.profileImageUrl;
  return /*#__PURE__*/jsxs("div", {
    className: "ntm-members-table__wrapper",
    children: [/*#__PURE__*/jsxs("div", {
      className: "ntm-members-table__column",
      children: [/*#__PURE__*/jsx(Avatar, {
        size: "medium",
        user: {
          name: name,
          imageUrl: imageUrl
        }
      }), /*#__PURE__*/jsx(Typography, {
        className: "whitespace-normal",
        "data-testid": "ntm-member-name-".concat(hyphenize(name)),
        style: "body2",
        children: name
      })]
    }), renderDropdownButton({
      config: config,
      handleUpdateRole: handleUpdateRole,
      handleUpdateStatus: handleUpdateStatus,
      tableData: tableData
    })]
  });
};
var renderDropdownButton = function renderDropdownButton(_ref2) {
  var _config$table, _config$table$dropdow;
  var config = _ref2.config,
    handleUpdateRole = _ref2.handleUpdateRole,
    handleUpdateStatus = _ref2.handleUpdateStatus,
    tableData = _ref2.tableData;
  return /*#__PURE__*/jsx(MoreDropdown, {
    dropdownProps: {
      appendTo: function appendTo() {
        return document.body;
      },
      strategy: "fixed"
    },
    isDisabled: not(config.permissions.update),
    dropdownButtonProps: {
      className: "me-1 my-1",
      "data-testid": "ntm-members-table-row-dropdown-button",
      tooltipProps: {
        content: i18next.t("neetoTeamMembers.tooltips.noPermissionToEditMembers"),
        disabled: config.permissions.update,
        position: "top"
      }
    },
    menuTopChildren: /*#__PURE__*/jsxs(Fragment, {
      children: [/*#__PURE__*/jsx(MenuItem.Button, {
        className: "ntm-dropdown__button",
        "data-testid": "ntm-edit-member-button",
        size: "small",
        onClick: function onClick() {
          return handleUpdateRole(tableData);
        },
        children: i18next.t("neetoTeamMembers.common.edit")
      }), config === null || config === void 0 || (_config$table = config.table) === null || _config$table === void 0 || (_config$table$dropdow = _config$table.dropdownProps) === null || _config$table$dropdow === void 0 ? void 0 : _config$table$dropdow.call(_config$table, tableData), /*#__PURE__*/jsx(MenuItem.Button, {
        className: "ntm-dropdown__button",
        "data-testid": "ntm-deactivate-member-button",
        size: "small",
        onClick: function onClick() {
          return handleUpdateStatus(tableData);
        },
        children: i18next.t("neetoTeamMembers.common.remove")
      })]
    })
  });
};
var getDefaultAlertTitle = function getDefaultAlertTitle(_ref3) {
  var member = _ref3.member;
  return member !== null && member !== void 0 && member.active ? i18next.t("neetoTeamMembers.common.remove_", {
    what: taxonomies.member.singular
  }) : i18next.t("neetoTeamMembers.common.activate_", {
    what: taxonomies.member.singular
  });
};
var getDefaultAlertMessage = function getDefaultAlertMessage(_ref4) {
  var member = _ref4.member;
  return /*#__PURE__*/jsx(Trans, {
    i18nKey: "neetoTeamMembers.alerts.messages.removeMember",
    values: _objectSpread(_objectSpread({}, SINGULAR), {}, {
      person: member === null || member === void 0 ? void 0 : member.displayName
    })
  });
};
var getDefaultColumns = function getDefaultColumns(_ref5) {
  var config = _ref5.config,
    handleUpdateRole = _ref5.handleUpdateRole,
    handleUpdateStatus = _ref5.handleUpdateStatus,
    otherColumns = _ref5.otherColumns,
    isSize = _ref5.isSize;
  return [{
    dataIndex: "name",
    key: "name",
    width: 300,
    fixed: isNotEmpty(otherColumns) && !isSize("mobile"),
    render: function render(_, tableData) {
      return renderNameColumn({
        config: config,
        handleUpdateRole: handleUpdateRole,
        handleUpdateStatus: handleUpdateStatus,
        tableData: tableData
      });
    },
    sorter: true,
    title: i18next.t("neetoTeamMembers.common.name"),
    isHidable: false
  }, {
    dataIndex: "email",
    key: "email",
    sorter: true,
    width: 350,
    title: i18next.t("neetoTeamMembers.common.email", SINGULAR)
  }, {
    dataIndex: "role",
    key: "organization_role",
    sorter: true,
    width: 200,
    title: i18next.t("neetoTeamMembers.common.role", SINGULAR)
  }];
};
var removeExcludedRole = curry(function (excludedRoles, role) {
  return !(excludedRoles !== null && excludedRoles !== void 0 && excludedRoles.includes(role === null || role === void 0 ? void 0 : role.name));
});
var getColumnData = function getColumnData(_ref6) {
  var _config$otherColumns;
  var config = _ref6.config,
    handleUpdateRole = _ref6.handleUpdateRole,
    handleUpdateStatus = _ref6.handleUpdateStatus,
    isSize = _ref6.isSize;
  var otherColumns = (_config$otherColumns = config === null || config === void 0 ? void 0 : config.otherColumns) !== null && _config$otherColumns !== void 0 ? _config$otherColumns : [];
  var defaultColumns = getDefaultColumns({
    config: config,
    handleUpdateRole: handleUpdateRole,
    handleUpdateStatus: handleUpdateStatus,
    otherColumns: otherColumns,
    isSize: isSize
  });
  var columns = defaultColumns.map(function (column) {
    var otherColumn = otherColumns.find(function (_ref7) {
      var dataIndex = _ref7.dataIndex;
      return identical(dataIndex, column.dataIndex);
    });
    return otherColumn || column;
  });
  otherColumns.forEach(function (otherColumn) {
    var column = columns.find(function (_ref8) {
      var dataIndex = _ref8.dataIndex;
      return identical(dataIndex, otherColumn.dataIndex);
    });
    if (!column) columns.push(otherColumn);
  });
  return columns;
};
var renderUpdateAlert = function renderUpdateAlert(field, props) {
  var _config$alert;
  var config = props.config,
    member = props.member;
  if (config !== null && config !== void 0 && (_config$alert = config.alert) !== null && _config$alert !== void 0 && _config$alert[field]) {
    var _config$alert2;
    return (_config$alert2 = config.alert) === null || _config$alert2 === void 0 ? void 0 : _config$alert2[field]({
      active: member === null || member === void 0 ? void 0 : member.active,
      id: member === null || member === void 0 ? void 0 : member.id,
      name: member === null || member === void 0 ? void 0 : member.displayName
    });
  }
  return field === "title" ? getDefaultAlertTitle(props) : getDefaultAlertMessage(props);
};
var handlePageChange = function handlePageChange(setFilters, history) {
  return function (page, pageSize) {
    var searchParams = getQueryParams({
      toCamelCase: false
    });
    history.replace(buildUrl(window.location.pathname, mergeLeft({
      page: page,
      page_size: pageSize
    }, searchParams)));
    setFilters(mergeLeft({
      page: page,
      pageSize: pageSize
    }));
  };
};
var renderPaginationProps = function renderPaginationProps(size) {
  return {
    pageSize: parseInt(size, DEFAULT_RADIX)
  };
};
var defaultNeetoFiltersColumns = function defaultNeetoFiltersColumns() {
  return [{
    key: "email",
    label: i18next.t("neetoTeamMembers.common.email", SINGULAR),
    model: "User",
    node: "email",
    showRuleSelector: true,
    type: "text"
  }, {
    key: "name",
    label: i18next.t("neetoTeamMembers.common.name"),
    model: "User",
    node: "first_name,last_name",
    type: "text",
    values: ""
  }, {
    key: "role",
    label: i18next.t("neetoTeamMembers.common.role", SINGULAR),
    model: "OrganizationRole",
    node: "organization_role.name",
    type: "multi_option",
    values: []
  }];
};
var buildFilterColumns = function buildFilterColumns(_ref9) {
  var _ref9$options = _ref9.options,
    _ref9$options$emails = _ref9$options.emails,
    emails = _ref9$options$emails === void 0 ? [] : _ref9$options$emails,
    _ref9$options$organiz = _ref9$options.organizationRoles,
    organizationRoles = _ref9$options$organiz === void 0 ? [] : _ref9$options$organiz,
    _ref9$emailConfig = _ref9.emailConfig,
    setSearchTerm = _ref9$emailConfig.setSearchTerm,
    isLoading = _ref9$emailConfig.isLoading;
  var columnValues = {
    email: emails.map(toLabelAndValue),
    role: organizationRoles.map(toLabelAndValue)
  };
  return defaultNeetoFiltersColumns().map(function (column) {
    var values = columnValues[column.key];
    if (column.key === "role") return assoc("values", values, column);
    var emailConfig = {
      values: values,
      isLoading: isLoading,
      onInputChange: setSearchTerm
    };
    return mergeLeft(emailConfig, column);
  });
};
var getFilteredRoles = function getFilteredRoles() {
  var roles = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  return filter(removeExcludedRole(config === null || config === void 0 ? void 0 : config.excludedRoles), roles);
};
var generateNoDataContent = function generateNoDataContent(_ref10) {
  var buttonProps = _ref10.buttonProps;
  var disabled = buttonProps.disabled,
    onClick = buttonProps.onClick;
  var noDataTitleTranslationKey = "notFound_";
  var noDataTitle = i18next.t("neetoTeamMembers.common.".concat(noDataTitleTranslationKey), {
    what: taxonomies.member.plural
  });
  var noDataButtonProps = {
    "data-testid": "ntm-add-member-button",
    disabled: disabled,
    label: i18next.t("neetoTeamMembers.common.addNew_", {
      what: taxonomies.member.singular
    }),
    onClick: onClick
  };
  return {
    noDataTitle: noDataTitle,
    noDataButtonProps: noDataButtonProps
  };
};

export { DEFAULT_FILTER_VALUES as D, DEFAULT_IS_OPEN_VALUES as a, buildFilterColumns as b, DEFAULT_SELECTED_ROWS as c, defaultNeetoFiltersColumns as d, getColumnData as e, generateNoDataContent as f, getFilteredRoles as g, handlePageChange as h, DEFAULT_RADIX as i, renderUpdateAlert as j, renderPaginationProps as r };
//# sourceMappingURL=utils-3WlshQ9e.js.map
