{{>licenseInfo}}
pragma Warnings (Off, "*is not referenced");
with {{openApiPackageName}}.Streams;
with {{openApiPackageName}}.Servers.Operation;
package body {{package}}.Skeletons is
   pragma Style_Checks ("-bmrIu");
   pragma Warnings (Off, "*use clause for package*");

   use {{openApiPackageName}}.Streams;

{{#mediaVariables}}
   {{name}} : aliased constant String := "{{value}}";
{{/mediaVariables}}
{{#mediaLists}}
   Media_List_{{-index}} : aliased constant {{openApiPackageName}}.Mime_List := ({{#this}}
     {{-index}} => {{name}}{{^-last}},
     {{/-last}}{{/this}});{{/mediaLists}}

   package body Skeleton is

{{#apiInfo}}
{{#apis}}
{{#operations}}
{{#operation}}

      package API_{{operationId}} is
         new {{openApiPackageName}}.Servers.Operation
            (Handler => {{operationId}},
             Method  => {{openApiPackageName}}.Servers.{{httpMethod}},
             URI     => URI_Prefix & "{{path}}",
{{#vendorExtensions.x-produces-media-index}}
             Mimes   => Media_List_{{vendorExtensions.x-produces-media-index}}'Access);
{{/vendorExtensions.x-produces-media-index}}
{{^vendorExtensions.x-produces-media-index}}
             Mimes   => null);
{{/vendorExtensions.x-produces-media-index}}

      --  {{summary}}
      procedure {{operationId}}
         (Req     : in out {{openApiPackageName}}.Servers.Request'Class;
          Reply   : in out {{openApiPackageName}}.Servers.Response'Class;
          Stream  : in out {{openApiPackageName}}.Servers.Output_Stream'Class;
          Context : in out {{openApiPackageName}}.Servers.Context_Type) is
         {{#hasBodyParam}}
         Input   : {{openApiPackageName}}.Value_Type;
         {{/hasBodyParam}}
         Impl : Implementation_Type;
         {{#allParams}}
         {{paramName}} : {{#vendorExtensions.x-ada-type-name}}{{.}}{{/vendorExtensions.x-ada-type-name}}{{^vendorExtensions.x-ada-type-name}}{{dataType}}{{/vendorExtensions.x-ada-type-name}};
         {{/allParams}}
         {{#returnProperty}}
         Result : {{#vendorExtensions.x-ada-type-name}}{{.}}{{/vendorExtensions.x-ada-type-name}}{{^vendorExtensions.x-ada-type-name}}{{dataType}}{{/vendorExtensions.x-ada-type-name}};
         {{/returnProperty}}
      begin
         {{#vendorExtensions.x-auth-scopes}}
         if not Context.Is_Authenticated then
            Context.Set_Error (401, "Not authenticated");
            return;
         end if;
         {{#scopes}}
         if not Context.Has_Permission (ACL_{{ident}}.Permission) then
            Context.Set_Error (403, "Permission denied");
            return;
         end if;
         {{/scopes}}
         {{/vendorExtensions.x-auth-scopes}}
         {{#queryParams}}{{#vendorExtensions.x-is-model-type}}
         {{paramName}} := To_{{dataType}} ({{openApiPackageName}}.Servers.Get_Query_Parameter (Req, "{{baseName}}"));
{{/vendorExtensions.x-is-model-type}}{{^vendorExtensions.x-is-model-type}}
         {{openApiPackageName}}.Servers.Get_Query_Parameter (Req, "{{baseName}}", {{paramName}});
{{/vendorExtensions.x-is-model-type}}{{/queryParams}}
         {{#pathParams}}{{#vendorExtensions.x-is-model-type}}
         {{paramName}} := To_{{dataType}} ({{openApiPackageName}}.Servers.Get_Path_Parameter (Req, {{vendorExtensions.x-path-index}}));
{{/vendorExtensions.x-is-model-type}}{{^vendorExtensions.x-is-model-type}}
         {{openApiPackageName}}.Servers.Get_Path_Parameter (Req, {{vendorExtensions.x-path-index}}, {{paramName}});
{{/vendorExtensions.x-is-model-type}}{{/pathParams}}
         {{#hasFormParams}}
         {{#formParams}}
         {{openApiPackageName}}.Servers.Get_Parameter (Context, "{{baseName}}", {{paramName}});
         {{/formParams}}
         {{/hasFormParams}}
         {{#hasParams}}
         {{#hasBodyParam}}
         {{openApiPackageName}}.Servers.Read (Req, Media_List_{{vendorExtensions.x-consumes-media-index}}, Input);
         if {{openApiPackageName}}.Is_Null (Input) then
            Context.Set_Error (415, "Invalid content");
            return;
         end if;
{{#bodyParams}}{{#vendorExtensions.x-is-model-type}}
         {{package}}.Models.Deserialize (Input, "{{baseName}}", {{paramName}});{{/vendorExtensions.x-is-model-type}}{{^vendorExtensions.x-is-model-type}}{{#isFile}}
         --  TODO: Serialize (Input.Stream, "{{basename}}", {{paramName}});{{/isFile}}{{^isFile}}{{^isLong}}
         Deserialize (Input, "{{baseName}}", {{paramName}});{{/isLong}}{{#isLong}}
         Deserialize (Input, "{{baseName}}", {{paramName}});{{/isLong}}{{/isFile}}{{/vendorExtensions.x-is-model-type}}{{/bodyParams}}
         {{/hasBodyParam}}
         Impl.{{operationId}}
            ({{#allParams}}{{paramName}}{{^-last}},
             {{/-last}}{{/allParams}}{{#returnType}}{{#hasParams}}, {{/hasParams}}Result{{/returnType}}, Context);
         {{/hasParams}}
         {{^hasParams}}
         {{#returnType}}
         Impl.{{operationId}} (Result, Context);
         {{/returnType}}
         {{^returnType}}
         Impl.{{operationId}} (Context);
         {{/returnType}}
         {{/hasParams}}
         {{#returnType}}
{{#responses}}
         if Context.Get_Status = {{code}} then{{#message}}
            Context.Set_Description ("{{message}}");{{/message}}{{#-first}}{{#schema}}{{#vendorExtensions.x-produces-plain-text}}{{#isFile}}
            {{openApiPackageName}}.Streams.Write (Stream, Result);
{{/isFile}}{{^isFile}}
            Stream.Write ({{openApiPackageName}}.To_String (Result));
{{/isFile}}{{/vendorExtensions.x-produces-plain-text}}
{{^vendorExtensions.x-produces-plain-text}}
{{#isFile}}
            {{openApiPackageName}}.Streams.Write (Stream, Result);
{{/isFile}}{{^isFile}}
            Stream.Start_Document;{{#vendorExtensions.x-is-stream-type}}{{^isLong}}{{#vendorExtensions.x-is-required}}
            Stream.{{vendorExtensions.x-ada-serialize-op}} ("", Result);{{/vendorExtensions.x-is-required}}{{^vendorExtensions.x-is-required}}{{#vendorExtensions.x-is-nullable}}
            Stream.{{vendorExtensions.x-ada-serialize-op}} ("", Result);{{/vendorExtensions.x-is-nullable}}{{^vendorExtensions.x-is-nullable}}
            if not {{#isFreeFormObject}}{{openApiPackageName}}.Is_Null (Result){{/isFreeFormObject}}{{^isFreeFormObject}}Value.{{name}}.Is_Null{{/isFreeFormObject}} then
               Stream.{{vendorExtensions.x-ada-serialize-op}} ("", Result);
            end if;{{/vendorExtensions.x-is-nullable}}{{/vendorExtensions.x-is-required}}{{/isLong}}{{#isLong}}{{#vendorExtensions.x-is-required}}
            Stream.{{vendorExtensions.x-ada-serialize-op}} ("", Result);{{/vendorExtensions.x-is-required}}{{^vendorExtensions.x-is-required}}{{#vendorExtensions.x-is-nullable}}
            Stream.{{vendorExtensions.x-ada-serialize-op}} ("", Result);{{/vendorExtensions.x-is-nullable}}{{^vendorExtensions.x-is-nullable}}
            if not Result.Is_Null then
               Stream.{{vendorExtensions.x-ada-serialize-op}} ("", Result);
            end if;{{/vendorExtensions.x-is-nullable}}{{/vendorExtensions.x-is-required}}{{/isLong}}{{/vendorExtensions.x-is-stream-type}}{{^vendorExtensions.x-is-stream-type}}
            Serialize (Stream, "", Result);{{/vendorExtensions.x-is-stream-type}}
            Stream.End_Document;{{/isFile}}{{/vendorExtensions.x-produces-plain-text}}{{/schema}}{{/-first}}
            return;
         end if;
{{/responses}}{{/returnType}}
      end {{operationId}};
{{/operation}}
{{/operations}}
{{/apis}}
{{/apiInfo}}

      procedure Register (Server : in out {{openApiPackageName}}.Servers.Application_Type'Class) is
      begin
{{#apiInfo}}
{{#apis}}
{{#operations}}
{{#operation}}
         {{openApiPackageName}}.Servers.Register (Server, API_{{operationId}}.Definition);
{{/operation}}
{{/operations}}
{{/apis}}
{{/apiInfo}}
      end Register;

   end Skeleton;

   package body Shared_Instance is

{{#apiInfo}}
{{#apis}}
{{#operations}}
{{#operation}}

      --  {{summary}}
      procedure {{operationId}}
         (Req     : in out {{openApiPackageName}}.Servers.Request'Class;
          Reply   : in out {{openApiPackageName}}.Servers.Response'Class;
          Stream  : in out {{openApiPackageName}}.Servers.Output_Stream'Class;
          Context : in out {{openApiPackageName}}.Servers.Context_Type) is
         {{#hasBodyParam}}
         Input   : {{openApiPackageName}}.Value_Type;
         {{/hasBodyParam}}
         {{#allParams}}
         {{paramName}} : {{#vendorExtensions.x-ada-type-name}}{{.}}{{/vendorExtensions.x-ada-type-name}}{{^vendorExtensions.x-ada-type-name}}{{dataType}}{{/vendorExtensions.x-ada-type-name}};
         {{/allParams}}
         {{#returnProperty}}
         Result : {{#vendorExtensions.x-ada-type-name}}{{.}}{{/vendorExtensions.x-ada-type-name}}{{^vendorExtensions.x-ada-type-name}}{{dataType}}{{/vendorExtensions.x-ada-type-name}};
         {{/returnProperty}}
      begin
         {{#vendorExtensions.x-auth-scopes}}
         if not Context.Is_Authenticated then
            Context.Set_Error (401, "Not authenticated");
            return;
         end if;
         {{#scopes}}
         if not Context.Has_Permission (ACL_{{ident}}.Permission) then
            Context.Set_Error (403, "Permission denied");
            return;
         end if;
         {{/scopes}}
         {{/vendorExtensions.x-auth-scopes}}
         {{#queryParams}}
{{#vendorExtensions.x-is-model-type}}
         {{paramName}} := To_{{dataType}} ({{openApiPackageName}}.Servers.Get_Query_Parameter (Req, "{{baseName}}"));
{{/vendorExtensions.x-is-model-type}}{{^vendorExtensions.x-is-model-type}}
         {{openApiPackageName}}.Servers.Get_Query_Parameter (Req, "{{baseName}}", {{paramName}});
{{/vendorExtensions.x-is-model-type}}{{/queryParams}}
         {{#pathParams}}{{#vendorExtensions.x-is-model-type}}
         {{paramName}} := To_{{dataType}} ({{openApiPackageName}}.Servers.Get_Path_Parameter (Req, {{vendorExtensions.x-path-index}}));
{{/vendorExtensions.x-is-model-type}}{{^vendorExtensions.x-is-model-type}}
         {{openApiPackageName}}.Servers.Get_Path_Parameter (Req, {{vendorExtensions.x-path-index}}, {{paramName}});
{{/vendorExtensions.x-is-model-type}}{{/pathParams}}
         {{#hasFormParams}}
         {{#formParams}}
         {{openApiPackageName}}.Servers.Get_Parameter (Context, "{{baseName}}", {{paramName}});
         {{/formParams}}
         {{/hasFormParams}}
         {{#hasParams}}
         {{#hasBodyParam}}
         {{openApiPackageName}}.Servers.Read (Req, Media_List_{{vendorExtensions.x-consumes-media-index}}, Input);
         {{#bodyParams}}{{#vendorExtensions.x-is-model-type}}
         {{package}}.Models.Deserialize (Input, "{{baseName}}", {{paramName}});{{/vendorExtensions.x-is-model-type}}{{^vendorExtensions.x-is-model-type}}{{#isFile}}
         --  TODO: Serialize (Input.Stream, "{{basename}}", {{paramName}});{{/isFile}}{{^isFile}}{{^isLong}}
         Deserialize (Input, "{{baseName}}", {{paramName}});{{/isLong}}{{#isLong}}
         Deserialize (Input, "{{baseName}}", {{paramName}});{{/isLong}}{{/isFile}}{{/vendorExtensions.x-is-model-type}}{{/bodyParams}}
         {{/hasBodyParam}}
         Server.{{operationId}}
            ({{#allParams}}{{paramName}}{{^-last}},
             {{/-last}}{{/allParams}}{{#returnType}}{{#hasParams}}, {{/hasParams}}Result{{/returnType}}, Context);
         {{/hasParams}}
         {{^hasParams}}
         {{#returnType}}
         Server.{{operationId}} (Result, Context);
         {{/returnType}}
         {{^returnType}}
         Server.{{operationId}} (Context);
         {{/returnType}}
         {{/hasParams}}
         {{#returnType}}
{{#responses}}
         if Context.Get_Status = {{code}} then{{#message}}
            Context.Set_Description ("{{message}}");{{/message}}{{#-first}}{{#schema}}{{#vendorExtensions.x-produces-plain-text}}{{#isFile}}
            {{openApiPackageName}}.Streams.Write (Stream, Result);
{{/isFile}}{{^isFile}}
            Stream.Write ({{openApiPackageName}}.To_String (Result));
{{/isFile}}{{/vendorExtensions.x-produces-plain-text}}
{{^vendorExtensions.x-produces-plain-text}}
{{#isFile}}
            {{openApiPackageName}}.Streams.Write (Stream, Result);
{{/isFile}}{{^isFile}}
            Stream.Start_Document;{{#vendorExtensions.x-is-stream-type}}{{^isLong}}{{#vendorExtensions.x-is-required}}
            Stream.{{vendorExtensions.x-ada-serialize-op}} ("", Result);{{/vendorExtensions.x-is-required}}{{^vendorExtensions.x-is-required}}{{#vendorExtensions.x-is-nullable}}
            Stream.{{vendorExtensions.x-ada-serialize-op}} ("", Result);{{/vendorExtensions.x-is-nullable}}{{^vendorExtensions.x-is-nullable}}
            if not {{#isFreeFormObject}}{{openApiPackageName}}.Is_Null (Result){{/isFreeFormObject}}{{^isFreeFormObject}}Value.{{name}}.Is_Null{{/isFreeFormObject}} then
               Stream.{{vendorExtensions.x-ada-serialize-op}} ("", Result);
            end if;{{/vendorExtensions.x-is-nullable}}{{/vendorExtensions.x-is-required}}{{/isLong}}{{#isLong}}{{#vendorExtensions.x-is-required}}
            Stream.{{vendorExtensions.x-ada-serialize-op}} ("", Result);{{/vendorExtensions.x-is-required}}{{^vendorExtensions.x-is-required}}{{#vendorExtensions.x-is-nullable}}
            Stream.{{vendorExtensions.x-ada-serialize-op}} ("", Result);{{/vendorExtensions.x-is-nullable}}{{^vendorExtensions.x-is-nullable}}
            if not Result.Is_Null then
               Stream.{{vendorExtensions.x-ada-serialize-op}} ("", Result);
            end if;{{/vendorExtensions.x-is-nullable}}{{/vendorExtensions.x-is-required}}{{/isLong}}{{/vendorExtensions.x-is-stream-type}}{{^vendorExtensions.x-is-stream-type}}
            Serialize (Stream, "", Result);{{/vendorExtensions.x-is-stream-type}}
            Stream.End_Document;{{/isFile}}{{/vendorExtensions.x-produces-plain-text}}{{/schema}}{{/-first}}
            return;
         end if;
{{/responses}}{{/returnType}}
      end {{operationId}};

      package API_{{operationId}} is
         new {{openApiPackageName}}.Servers.Operation
            (Handler => {{operationId}},
             Method  => {{openApiPackageName}}.Servers.{{httpMethod}},
             URI     => URI_Prefix & "{{path}}",
{{#vendorExtensions.x-produces-media-index}}
             Mimes   => Media_List_{{vendorExtensions.x-produces-media-index}}'Access);
{{/vendorExtensions.x-produces-media-index}}
{{^vendorExtensions.x-produces-media-index}}
             Mimes   => null);
{{/vendorExtensions.x-produces-media-index}}

{{/operation}}
{{/operations}}
{{/apis}}
{{/apiInfo}}

      procedure Register (Server : in out {{openApiPackageName}}.Servers.Application_Type'Class) is
      begin
{{#apiInfo}}
{{#apis}}
{{#operations}}
{{#operation}}
         {{openApiPackageName}}.Servers.Register (Server, API_{{operationId}}.Definition);
{{/operation}}
{{/operations}}
{{/apis}}
{{/apiInfo}}
      end Register;

      protected body Server is
{{#apiInfo}}
{{#apis}}
{{#operations}}
{{#operation}}
         --  {{summary}}
         {{#hasParams}}
         procedure {{operationId}}
            ({{#allParams}}{{paramName}} : in {{#vendorExtensions.x-ada-type-name}}{{.}}{{/vendorExtensions.x-ada-type-name}}{{^vendorExtensions.x-ada-type-name}}{{dataType}}{{/vendorExtensions.x-ada-type-name}}{{^-last}};
             {{/-last}}{{/allParams}}{{#returnProperty}};
             Result : out {{#vendorExtensions.x-ada-type-name}}{{.}}{{/vendorExtensions.x-ada-type-name}}{{^vendorExtensions.x-ada-type-name}}{{dataType}}{{/vendorExtensions.x-ada-type-name}}{{/returnProperty}};
             Context : in out {{openApiPackageName}}.Servers.Context_Type) is
         begin
            Impl.{{operationId}}
               ({{#allParams}}{{paramName}}{{^-last}},
                {{/-last}}{{/allParams}}{{#returnType}},
                Result{{/returnType}},
                Context);
         end {{operationId}};
         {{/hasParams}}
         {{^hasParams}}
         {{#returnType}}
         procedure {{operationId}} (Result : out {{returnProperty.vendorExtensions.x-ada-type-name}};
         Context : in out {{openApiPackageName}}.Servers.Context_Type) is
         begin
            Impl.{{operationId}} (Result, Context);
         end {{operationId}};
         {{/returnType}}
         {{^returnType}}
         procedure {{operationId}} (Context : in out {{openApiPackageName}}.Servers.Context_Type) is
         begin
            Impl.{{operationId}} (Context);
         end {{operationId}};
         {{/returnType}}
         {{/hasParams}}

{{/operation}}
{{/operations}}
{{/apis}}
{{/apiInfo}}
      end Server;

   end Shared_Instance;

end {{package}}.Skeletons;
