"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.canEditItem = canEditItem;
const get_prop_1 = require("../objects/get-prop");
const has_base_priv_1 = require("./has-base-priv");
const includes_1 = require("../utils/includes");
const is_update_group_1 = require("../utils/is-update-group");
/**
 * Checks if user has access to edit an item in Hub
 * @param {IItem} item
 * @param {IUser} user
 * @returns {boolean}
 */
function canEditItem(item, user) {
    let res = false;
    const itemControls = ["admin", "update"];
    const { itemControl, owner, orgId: itemOrgId } = item;
    const { roleId, role, username, groups: userGroups, orgId: userOrgId } = user;
    const hasItemControl = (0, includes_1.includes)(itemControls, itemControl);
    const isOwner = !!owner && owner === username;
    const isOrgItem = !!itemOrgId && itemOrgId === userOrgId;
    const isItemOrgAdmin = !!isOrgItem && !roleId && role === "org_admin";
    const hasPlatformControl = hasItemControl || isOwner || isItemOrgAdmin;
    const hasPriv = (0, has_base_priv_1.hasBasePriv)(user);
    if (hasPriv && hasPlatformControl) {
        res = true;
    }
    else if (hasPriv) {
        const itemGroups = [
            ...((0, get_prop_1.getProp)(item, "groupIds") || []),
            (0, get_prop_1.getProp)(item, "properties.collaborationGroupId"),
        ];
        const isGroupEditable = (group) => (0, is_update_group_1.isUpdateGroup)(group) && (0, includes_1.includes)(itemGroups, group.id);
        res = userGroups.some(isGroupEditable);
    }
    return res;
}
//# sourceMappingURL=can-edit-item.js.map