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