export const createActivity = async (activityTypeId, comment, objectId) => {
    const activityInfo = {
        activityTypeId, // Can be activity type name or numeric id
        comment: comment ? comment : '',
        objectId: objectId ? objectId : 0
    };

    const response = await fetch('/CIQDotNet/CreditAnalytics/CAChartService.asmx/activityCreate', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify(activityInfo),
        cache: 'no-store'
      });
      return response.json();
};

export const openAddCompanyPopup = ({
    countryCode, industry, csCompanyId,
    companyType, companyName, state,
    employeeCount, yearFounded
}, messageListener) => {
    const paramsObject = {
        Source: 'SUI',
        ...(countryCode && { CountryCode: countryCode }),
        ...(state && { State: state }),
        ...(industry && { industry }),
        ...(csCompanyId && { CsCompanyId: csCompanyId }),
        ...(countryCode && { CountryCode: countryCode }),
        ...(companyType && { CompanyType: ['1', '3'].includes(companyType) ? '4' : '5' }),
        ...(companyName && { NewCompanyName: encodeURIComponent(companyName) }),
        ...(employeeCount && { employeeCount }),
        ...(yearFounded && { yearFounded })
    };
    console.log('_debug openAddCompanyPopup:', paramsObject);
    const queryString = Object.keys(paramsObject).map(key => `${key}=${paramsObject[key]}`).join('&');
    window.AddToListModalPopupPanelJsInstance.show(`/CIQDotNet/company/addcompany.aspx?${queryString}`, 'CreateProp');
    window.AddToListModalPopupPanelJsInstance.changeTitle('Add a New Company');
    window.removeEventListener('message', messageListener); //Clean up existing listener, if any
    window.addEventListener('message', messageListener);
    createActivity('CA_SUI_NoCompanyPage_PropUpload'); // note: this doesn't capture if the user successfully uploads
};
