/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{save as e,saveAs as r}from"./utils.js";import{createForItemWrite as t}from"../../portal/support/jsonContext.js";import{typeKeyword as o,hasTypeKeyword as a,removeTypeKeyword as s,addTypeKeyword as n}from"../../portal/support/portalItemUtils.js";import{saveResources as i}from"../../webdoc/support/resourceUtils.js";const u="Group Layer",c="group-layer-save",l="group-layer-save-as",p=o.GROUP_LAYER_MAP;function m(e){return{isValid:"group"===e.type,errorMessage:"Layer.type should be 'group'"}}function y(e){return{isValid:a(e,p),errorMessage:`Layer.portalItem.typeKeywords should have '${p}'`}}function f(e,r){return{...t(e,"web-map",!0),initiator:r}}function v(e){const r=e.layerJSON;return Promise.resolve(r&&Object.keys(r).length?r:null)}async function d(e,r){r.title||=e.title,s(r,o.METADATA),n(r,p)}async function I(r,t){return e({layer:r,itemType:u,validateLayer:m,validateItem:y,createJSONContext:e=>f(e,r),createItemData:v,errorNamePrefix:c,saveResources:async(e,t)=>(r.sourceIsPortalItem||await e.removeAllResources().catch(()=>{}),i(r.resourceReferences,t))},t)}async function g(e,t,o){return r({layer:e,itemType:u,validateLayer:m,createJSONContext:r=>f(r,e),createItemData:v,errorNamePrefix:l,newItem:t,setItemProperties:d,saveResources:(r,t)=>i(e.resourceReferences,t)},o)}export{I as save,g as saveAs};
