const RESERVED_PATH_PARAMETERS = ['version', 'udp-api-version'];
export const getPrimaryKeyValues = (properties, entityValue) => {
    const primaryKeyProperties = properties === null || properties === void 0 ? void 0 : properties.filter((property) => {
        var _a;
        return (_a = property === null || property === void 0 ? void 0 : property.extendedProperties) === null || _a === void 0 ? void 0 : _a.identifier;
    });
    const primaryKeyValues = primaryKeyProperties === null || primaryKeyProperties === void 0 ? void 0 : primaryKeyProperties.reduce((keyValues, property) => {
        if (entityValue && entityValue[property['name']] !== null) {
            keyValues.push(entityValue[property['name']]);
        }
        return keyValues;
    }, []);
    return primaryKeyValues;
};
export const getPathObject = (pathParameters, primaryKeyValues) => {
    const primaryKeyParameters = pathParameters.filter((parameter) => parameter.name !== RESERVED_PATH_PARAMETERS);
    const pathObject = {};
    if (primaryKeyParameters.length && primaryKeyValues.length) {
        primaryKeyParameters.forEach((pkp, index) => {
            pathObject[`{${pkp.name}}`] = primaryKeyValues[index];
        });
    }
    return pathObject;
};
export const findTermByType = (fieldType, term) => {
    if (fieldType.toLowerCase() === term) {
        return fieldType.toLowerCase();
    }
    else {
        return '';
    }
};
const getModelProperties = (model, componentsObj) => {
    var _a;
    let modelPropertiesObj = null;
    if (model) {
        modelPropertiesObj = model.properties;
        if (!modelPropertiesObj) {
            const refKeys = [];
            if ((_a = model['allOf']) === null || _a === void 0 ? void 0 : _a.length) {
                model['allOf'].forEach((reference) => {
                    if (reference['$ref']) {
                        const refString = reference['$ref'];
                        if (refString) {
                            const lastIndex = refString.lastIndexOf('/');
                            const referenceModel = refString.substring(lastIndex + 1);
                            refKeys.push(referenceModel);
                        }
                    }
                });
            }
            refKeys.forEach((key) => {
                const abstractModel = componentsObj[key];
                if (modelPropertiesObj && (abstractModel === null || abstractModel === void 0 ? void 0 : abstractModel.properties)) {
                    modelPropertiesObj = Object.assign(Object.assign({}, modelPropertiesObj), abstractModel.properties);
                }
                else {
                    modelPropertiesObj = abstractModel === null || abstractModel === void 0 ? void 0 : abstractModel.properties;
                }
            });
        }
    }
    return modelPropertiesObj;
};
export const getEntityAttributes = (catalog, entityName) => {
    var _a, _b, _c;
    if (!catalog) {
        return [];
    }
    const catalogObjectList = (_a = catalog === null || catalog === void 0 ? void 0 : catalog.ctlg) === null || _a === void 0 ? void 0 : _a.catalogObjectList;
    const componentsObj = (_b = catalog === null || catalog === void 0 ? void 0 : catalog.ctlg) === null || _b === void 0 ? void 0 : _b.components;
    const entity = catalogObjectList === null || catalogObjectList === void 0 ? void 0 : catalogObjectList.find((catalogObject) => catalogObject.catalogObjectName === entityName);
    if (!!componentsObj && Object.keys(componentsObj).length) {
        const model = componentsObj[entityName];
        let modelPropertiesObj = getModelProperties(model, componentsObj);
        if ((_c = entity === null || entity === void 0 ? void 0 : entity.properties) === null || _c === void 0 ? void 0 : _c.length) {
            entity.properties.forEach((property) => {
                let nullable = true;
                if (modelPropertiesObj) {
                    const modelProperty = modelPropertiesObj[property['name']];
                    if (modelProperty) {
                        nullable = modelProperty.nullable || false;
                    }
                }
                property['nullable'] = nullable;
            });
        }
    }
    return entity ? entity.properties : [];
};
export const getPathParametersObj = (pathParametersDefinition, entityData) => {
    if (!pathParametersDefinition || !entityData)
        return {};
    const pathParametersObj = {};
    for (let param of pathParametersDefinition) {
        pathParametersObj[param.name] = entityData[param.name];
    }
    return pathParametersObj;
};
//# sourceMappingURL=CrudBrowserUtilities.js.map