'use strict';

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var i18next = require('i18next');
var neetoCist = require('@bigbinary/neeto-cist');
var constants = require('@bigbinary/neeto-commons-frontend/constants');
var initializers = require('@bigbinary/neeto-commons-frontend/initializers');
var utils = require('@bigbinary/neeto-commons-frontend/utils');
var MoreDropdown = require('@bigbinary/neeto-molecules/MoreDropdown');
var Avatar = require('@bigbinary/neetoui/Avatar');
var Dropdown = require('@bigbinary/neetoui/Dropdown');
var Typography = require('@bigbinary/neetoui/Typography');
var ramda = require('ramda');
var reactI18next = require('react-i18next');
var jsxRuntime = require('react/jsx-runtime');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);
var i18next__default = /*#__PURE__*/_interopDefault(i18next);
var MoreDropdown__default = /*#__PURE__*/_interopDefault(MoreDropdown);
var Avatar__default = /*#__PURE__*/_interopDefault(Avatar);
var Dropdown__default = /*#__PURE__*/_interopDefault(Dropdown);
var Typography__default = /*#__PURE__*/_interopDefault(Typography);

var _getQueryParams = utils.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: ramda.clamp(1, Infinity, parseInt(page)) || constants.DEFAULT_PAGE_INDEX,
  pageSize: ramda.clamp(10, 100, parseInt(pageSize)) || constants.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__default.default(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__default.default.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__*/jsxRuntime.jsxs("div", {
    className: "ntm-members-table__wrapper",
    children: [/*#__PURE__*/jsxRuntime.jsxs("div", {
      className: "ntm-members-table__column",
      children: [/*#__PURE__*/jsxRuntime.jsx(Avatar__default.default, {
        size: "medium",
        user: {
          name: name,
          imageUrl: imageUrl
        }
      }), /*#__PURE__*/jsxRuntime.jsx(Typography__default.default, {
        className: "whitespace-normal",
        "data-testid": "ntm-member-name-".concat(utils.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__*/jsxRuntime.jsx(MoreDropdown__default.default, {
    dropdownProps: {
      appendTo: function appendTo() {
        return document.body;
      },
      strategy: "fixed"
    },
    isDisabled: ramda.not(config.permissions.update),
    dropdownButtonProps: {
      className: "me-1 my-1",
      "data-testid": "ntm-members-table-row-dropdown-button",
      tooltipProps: {
        content: i18next__default.default.t("neetoTeamMembers.tooltips.noPermissionToEditMembers"),
        disabled: config.permissions.update,
        position: "top"
      }
    },
    menuTopChildren: /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, {
      children: [/*#__PURE__*/jsxRuntime.jsx(MenuItem.Button, {
        className: "ntm-dropdown__button",
        "data-testid": "ntm-edit-member-button",
        size: "small",
        onClick: function onClick() {
          return handleUpdateRole(tableData);
        },
        children: i18next__default.default.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__*/jsxRuntime.jsx(MenuItem.Button, {
        className: "ntm-dropdown__button",
        "data-testid": "ntm-deactivate-member-button",
        size: "small",
        onClick: function onClick() {
          return handleUpdateStatus(tableData);
        },
        children: i18next__default.default.t("neetoTeamMembers.common.remove")
      })]
    })
  });
};
var getDefaultAlertTitle = function getDefaultAlertTitle(_ref3) {
  var member = _ref3.member;
  return member !== null && member !== void 0 && member.active ? i18next__default.default.t("neetoTeamMembers.common.remove_", {
    what: initializers.taxonomies.member.singular
  }) : i18next__default.default.t("neetoTeamMembers.common.activate_", {
    what: initializers.taxonomies.member.singular
  });
};
var getDefaultAlertMessage = function getDefaultAlertMessage(_ref4) {
  var member = _ref4.member;
  return /*#__PURE__*/jsxRuntime.jsx(reactI18next.Trans, {
    i18nKey: "neetoTeamMembers.alerts.messages.removeMember",
    values: _objectSpread(_objectSpread({}, constants.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: neetoCist.isNotEmpty(otherColumns) && !isSize("mobile"),
    render: function render(_, tableData) {
      return renderNameColumn({
        config: config,
        handleUpdateRole: handleUpdateRole,
        handleUpdateStatus: handleUpdateStatus,
        tableData: tableData
      });
    },
    sorter: true,
    title: i18next__default.default.t("neetoTeamMembers.common.name"),
    isHidable: false
  }, {
    dataIndex: "email",
    key: "email",
    sorter: true,
    width: 350,
    title: i18next__default.default.t("neetoTeamMembers.common.email", constants.SINGULAR)
  }, {
    dataIndex: "role",
    key: "organization_role",
    sorter: true,
    width: 200,
    title: i18next__default.default.t("neetoTeamMembers.common.role", constants.SINGULAR)
  }];
};
var removeExcludedRole = ramda.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 ramda.identical(dataIndex, column.dataIndex);
    });
    return otherColumn || column;
  });
  otherColumns.forEach(function (otherColumn) {
    var column = columns.find(function (_ref8) {
      var dataIndex = _ref8.dataIndex;
      return ramda.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 = utils.getQueryParams({
      toCamelCase: false
    });
    history.replace(utils.buildUrl(window.location.pathname, ramda.mergeLeft({
      page: page,
      page_size: pageSize
    }, searchParams)));
    setFilters(ramda.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__default.default.t("neetoTeamMembers.common.email", constants.SINGULAR),
    model: "User",
    node: "email",
    showRuleSelector: true,
    type: "text"
  }, {
    key: "name",
    label: i18next__default.default.t("neetoTeamMembers.common.name"),
    model: "User",
    node: "first_name,last_name",
    type: "text",
    values: ""
  }, {
    key: "role",
    label: i18next__default.default.t("neetoTeamMembers.common.role", constants.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(neetoCist.toLabelAndValue),
    role: organizationRoles.map(neetoCist.toLabelAndValue)
  };
  return defaultNeetoFiltersColumns().map(function (column) {
    var values = columnValues[column.key];
    if (column.key === "role") return ramda.assoc("values", values, column);
    var emailConfig = {
      values: values,
      isLoading: isLoading,
      onInputChange: setSearchTerm
    };
    return ramda.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 ramda.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__default.default.t("neetoTeamMembers.common.".concat(noDataTitleTranslationKey), {
    what: initializers.taxonomies.member.plural
  });
  var noDataButtonProps = {
    "data-testid": "ntm-add-member-button",
    disabled: disabled,
    label: i18next__default.default.t("neetoTeamMembers.common.addNew_", {
      what: initializers.taxonomies.member.singular
    }),
    onClick: onClick
  };
  return {
    noDataTitle: noDataTitle,
    noDataButtonProps: noDataButtonProps
  };
};

exports.DEFAULT_FILTER_VALUES = DEFAULT_FILTER_VALUES;
exports.DEFAULT_IS_OPEN_VALUES = DEFAULT_IS_OPEN_VALUES;
exports.DEFAULT_RADIX = DEFAULT_RADIX;
exports.DEFAULT_SELECTED_ROWS = DEFAULT_SELECTED_ROWS;
exports.buildFilterColumns = buildFilterColumns;
exports.defaultNeetoFiltersColumns = defaultNeetoFiltersColumns;
exports.generateNoDataContent = generateNoDataContent;
exports.getColumnData = getColumnData;
exports.getFilteredRoles = getFilteredRoles;
exports.handlePageChange = handlePageChange;
exports.renderPaginationProps = renderPaginationProps;
exports.renderUpdateAlert = renderUpdateAlert;
//# sourceMappingURL=utils-CgwJI9pk.js.map
