import{ArgumentError as t}from"../../ArgumentError.js";import{InvalidOperationError as e}from"../../InvalidOperationError.js";import{isLayerExtension as r,isSublayerExtension as a,isTableExtension as o}from"../../ItemType.js";import{DefaultArcGISRequestHelper as s}from"../../support/ArcGISRequestHelper.js";import{isSublayer as n,isWFSLayer as i}from"../../utilities/_layers.js";import{checkArg as c}from"../../utilities/checkArg.js";import{isCSVLayerExtension as p,isFeatureLayerExtension as m,isGeoJSONLayerExtension as h,isMapImageSublayerExtension as l,isSceneLayerExtension as u,isSubtypeGroupLayerExtension as d,isSubtypeSublayerExtension as f,isWFSLayerExtension as y}from"../../utilities/extensions.js";import{isCSVLayer as w,isFeatureLayer as A,isGeoJSONLayer as E,isSceneLayer as T,isSubtypeGroupLayer as b,isSubtypeSublayer as g}from"../../utilities/layers.js";const x=new s;export async function toAttachmentFile(t,e){let r;if(t instanceof File)r=t;else if(t?.url)try{const e=await x.request(t.url,{responseType:"blob"});r=new File([e.data],t.title??"",{type:t.contentType})}catch(t){throw new Error((e?`${e} `:"")+"Failed to fetch attachment from url.",{cause:t})}if(!r)throw new Error((e?`${e} `:"")+"No attachment data found.");return r}export function supportsAttachments(t){if(o(t))return!!t.table.capabilities?.data?.supportsAttachment;if(l(t))return!!t.sublayer.capabilities?.data?.supportsAttachment;let e=t;return(f(t)||a(t))&&(e=t.layerExtension),(m(e)||d(e)||p(e)||h(e)||u(e)||y(e))&&!!e.layer.capabilities?.data?.supportsAttachment}export function supportsAttachmentEditing(t){const e=g(t)||n(t)?t.layer:t;return(A(e)||b(e)||w(e)||E(e)||T(e)||i(e))&&!!e.capabilities?.data?.supportsAttachment&&(e.capabilities.operations?.supportsUpdate||e.capabilities.operations?.supportsAdd)}export async function addAttachment(a,s,n,i){if(c("feature",a).isDefined(),c("layer",s).isDefined(),c("attachment",n).isDefined(),r(a.source)&&a.source.layer!==s||o(a.source)&&a.source.table!==s)throw new t("layer","The feature's source layer/table does not match the layer.");if(!supportsAttachmentEditing(s))throw new e("Cannot add attachment; the Table does not support attachments.");const p=`Failed to add attachment to ${a.primaryKey??a.id} of source ${a.source?.title??a.source?.id}.`,m=await toAttachmentFile(n,p),h=m.name?.split(".").pop()?.toLowerCase();if(i?.attachmentTypes&&!function(t,e){if(!e.map((t=>t.toLowerCase())).includes(t.type.toLowerCase())){const r=e.filter((t=>!t.includes("/"))).map((t=>t.toLowerCase())),a=t.name?.split(".").pop()?.toLowerCase();return null!=a&&r.includes(a)}return!0}(m,i.attachmentTypes))throw new t("attachment",`The attachment type '${h}' does not exist in the set of accepted attachment types: ${i.attachmentTypes}.`);let l;const u=new FormData;u.append("attachment",m,m.name);try{const t=await s.addAttachment(a.toGraphic(),u);if(t.error)throw new Error(t.error.message);l=`${t.objectId}`}catch(t){throw new Error(p,{cause:t})}return await a.fetchAttachments(),a.attachments.find((t=>t.id===l))}export function isAttachmentTypeError(t){return ATTACHMENT_TYPE_ERROR_REGEX.test(t.message)}export const ATTACHMENT_TYPE_ERROR_REGEX=/^The attachment type .+ does not exist in the set of accepted attachment types: .+.$/;export async function deleteAttachments(a,s,n){if(c("feature",a).isDefined(),c("layer",s).isDefined(),c("attachments",n).isDefined(),r(a.source)&&a.source.layer!==s||o(a.source)&&a.source.table!==s)throw new t("layer","The feature's source layer/table does not match the layer.");if(!supportsAttachmentEditing(s))throw new e("Cannot delete attachments; the layer/table does not support attachments.");const i=`Failed to delete attachment(s) from ${a.primaryKey??a.id} of source ${a.source?.title??a.source?.id}.`;try{const t=await s.deleteAttachments(a.toGraphic(),n.map((t=>"string"==typeof t?+t:+t.id)));if(t?.length>1){const e=t.filter((t=>t.error));if(e.length>0)throw new AggregateError(e.map((t=>new Error(t.error?.message))))}else if(t?.[0]?.error)throw new Error(t?.[0].error.message)}catch(t){throw new Error(i,{cause:t})}await a.fetchAttachments()}