import { CmsFieldTypePlugins , CmsModel } from "~/types" ; import { createManageTypeName , createTypeName } from "~/content/plugins/utils/createTypeName" ; import { renderListFilterFields } from "~/content/plugins/utils/renderListFilterFields" ; import { renderSortEnum } from "~/content/plugins/utils/renderSortEnum" ; import { renderGetFilterFields } from "~/content/plugins/utils/renderGetFilterFields" ; import { renderInputFields } from "~/content/plugins/utils/renderInputFields" ; import { renderFields } from "~/content/plugins/utils/renderFields" ; import { pluralizedTypeName } from "~/content/plugins/utils/pluralizedTypeName" ; interface CreateManageSDL { ( params ) : string ; } export const createManageSDL = ( { model , fieldTypePlugins } ) : string => { const typeName = createTypeName ( model . modelId ) ; const mTypeName = createManageTypeName ( typeName ) ; const listFilterFieldsRender = renderListFilterFields ( { model , type : "manage" , fieldTypePlugins } ) ; const sortEnumRender = renderSortEnum ( { model , fieldTypePlugins } ) ; const getFilterFieldsRender = renderGetFilterFields ( { model , fieldTypePlugins } ) ; const inputFields = renderInputFields ( { model , fieldTypePlugins } ) ; const fields = renderFields ( { model , type : "manage" , fieldTypePlugins } ) ; if ( inputFields . length === 0 ) { return "" ; } return `
        """${ model . description || model . modelId }"""
        ${ fields . map ( f => f . typeDefs ) . filter ( Boolean ) . join ( "\n" ) }

        type ${ mTypeName } {
            id: ID!
            entryId: String!
            createdOn: DateTime!
            savedOn: DateTime!
            createdBy: CmsCreatedBy!
            ownedBy: CmsOwnedBy!
            meta: ${ mTypeName }Meta
            ${ fields . map ( f => f . fields ) . join ( "\n" ) }
        }

        type ${ mTypeName }Meta {
            modelId: String
            version: Int
            locked: Boolean
            publishedOn: DateTime
            status: String
            """
            CAUTION: this field is resolved by making an extra query to DB. 
            RECOMMENDATION: Use it only with "get" queries (avoid in "list") 
            """
            revisions: [${ mTypeName }]
            title: String
        }
        
                    
        ${ inputFields . map ( f => f . typeDefs ) . filter ( Boolean ) . join ( "\n" ) }

        ${ inputFields && `input ${ mTypeName }Input {
            ${ inputFields . map ( f => f . fields ) . join ( "\n" ) }
        }` }

        ${ getFilterFieldsRender && `input ${ mTypeName }GetWhereInput {
            ${ getFilterFieldsRender }
        }` }


        ${ listFilterFieldsRender && `input ${ mTypeName }ListWhereInput {
            ${ listFilterFieldsRender }
        }` }

        type ${ mTypeName }Response {
            data: ${ mTypeName }
            error: CmsError
        }
        
        type ${ mTypeName }ArrayResponse {
            data: [${ mTypeName }]
            error: CmsError
        }

        type ${ mTypeName }ListResponse {
            data: [${ mTypeName }]
            meta: CmsListMeta
            error: CmsError
        }

        ${ sortEnumRender && `enum ${ mTypeName }ListSorter {
            ${ sortEnumRender }
        }` }

        extend type Query {
            get${ typeName }(revision: ID!): ${ mTypeName }Response
            
            get${ typeName }Revisions(id: ID!): ${ mTypeName }ArrayResponse
            
            get${ pluralizedTypeName ( typeName ) }ByIds(revisions: [ID!]!): ${ mTypeName }ArrayResponse

            list${ pluralizedTypeName ( typeName ) }(
                where: ${ mTypeName }ListWhereInput
                sort: [${ mTypeName }ListSorter]
                limit: Int
                after: String
            ): ${ mTypeName }ListResponse
        }

        extend type Mutation{
            create${ typeName }(data: ${ mTypeName }Input!): ${ mTypeName }Response

            create${ typeName }From(revision: ID!, data: ${ mTypeName }Input): ${ mTypeName }Response

            update${ typeName }(revision: ID!, data: ${ mTypeName }Input!): ${ mTypeName }Response

            delete${ typeName }(revision: ID!): CmsDeleteResponse

            publish${ typeName }(revision: ID!): ${ mTypeName }Response
        
            republish${ typeName }(revision: ID!): ${ mTypeName }Response

            unpublish${ typeName }(revision: ID!): ${ mTypeName }Response
            
            request${ typeName }Review(revision: ID!): ${ mTypeName }Response
            
            request${ typeName }Changes(revision: ID!): ${ mTypeName }Response
        }
    ` ; } ;