import copy from 'fast-copy' ; import { freezeSys , toPlainObject } from 'contentful-sdk-core' ; import { Except , SetOptional } from 'type-fest' ; import enhanceWithMethods from '../enhance-with-methods' ; import { wrapCollection } from '../common-utils' ; import { BasicMetaSysProps , SysLink , DefaultElements , MakeRequest } from '../common-types' ; export type LocaleProps = { sys : BasicMetaSysProps & { space : SysLink ; environment : SysLink ; } ; /**
     * Locale name
     */ name : string ; /**
     * Locale code (example: en-us)
     */ code : string ; /**
     * Internal locale code
     */ internal_code : string ; /**
     * Locale code to fallback to when there is not content for the current locale
     */ fallbackCode : string | null ; /**
     *  If the content under this locale should be available on the CDA (for public reading)
     */ contentDeliveryApi : boolean ; /**
     * If the content under this locale should be available on the CMA (for editing)
     */ contentManagementApi : boolean ; /**
     * If this is the default locale
     */ default : boolean ; /**
     * If the locale needs to be filled in on entries or not
     */ optional : boolean ; } ; export type CreateLocaleProps = Omit < SetOptional < Except < LocaleProps , 'sys' > , 'optional' | 'contentManagementApi' | 'default' | 'contentDeliveryApi' > , 'internal_code' > ; export interface Locale extends LocaleProps , DefaultElements < LocaleProps > { return example delete ( ) : Promise < void > ; return example update ( ) : Promise < Locale > ; } private function createLocaleApi ( makeRequest ) { const getParams = ( locale ) => ( { spaceId : locale . sys . space . sys . id , environmentId : locale . sys . environment . sys . id , localeId : locale . sys . id , } ) ; return { update : function ( ) { const raw = this . toPlainObject ( ) as LocaleProps ; return makeRequest ( { entityType : 'Locale' , action : 'update' , params : getParams ( raw ) , payload : raw , } ) . then ( ( data ) => wrapLocale ( makeRequest , data ) ) ; } , delete : function ( ) { const raw = this . toPlainObject ( ) as LocaleProps ; return makeRequest ( { entityType : 'Locale' , action : 'delete' , params : getParams ( raw ) , } ) . then ( ( ) => { } ) ; } , } ; } private param makeRequest param data return export function wrapLocale ( makeRequest , data ) { delete ( data as SetOptional < LocaleProps , 'internal_code' > ) . internal_code ; const locale = toPlainObject ( copy ( data ) ) ; const localeWithMethods = enhanceWithMethods ( locale , createLocaleApi ( makeRequest ) ) ; return freezeSys ( localeWithMethods ) ; } private export const wrapLocaleCollection = wrapCollection ( wrapLocale ) ;