<commit_before>using System;
using System.Linq;
using System.Text.RegularExpressions;
using cstsd.Core.Extensions;
using cstsd.Core.Net;
using cstsd.Core.Ts;
using System.Collections.Generic;


namespace cstsd.TypeScript
{
    //TODO: rename to API generator?
    public class NetCsControllerConverter
    {
        public virtual NetClass GetControllerApiClientCsClass(NetClass controllerNetClass)
        {
            var controllerMethods = controllerNetClass
                .Methods
                .ToList();
            
            return new NetClass
            {
                Name = controllerNetClass.Name,
                Methods = controllerMethods
                    .Where(m => m.ReturnType.Name != "IActionResult")
                    .Where(m => m.IsPublic && m.Attributes.Any(a => a == "CsExport"))
                    .Select(a => GetCsProxy(a, controllerNetClass.Name))
                    .ToList(),
                Fields =  GetFields(controllerMethods, controllerNetClass),
                IsPublic = true,
                Constructor = GetConstructor(controllerNetClass)
            };
        }

        private static NetMethod GetConstructor(NetClass controllerNetClass)
        {
            return new NetMethod
            {
                IsPublic = true,
                Parameters = new[]
                {
                    new NetParameter
                    {
                        FieldType = new NetType
                        {
                            Name = "string"
                        },
                        Name = "baseUrl"
                    }
                },
                MethodBody = "_baseUrl = baseUrl;"
            };
        }

        private static string GetControllerName(string controllerName)
        {
            if (controllerName.EndsWith("Controller"))
                return controllerName.Substring(0, controllerName.Length - "Controller".Length);

            return controllerName;
        }

        private ICollection<NetField> GetFields(IReadOnlyCollection<NetMethod> controllerMethods, NetClass controllerNetClass)
        {
            var distinctControllerMethods = controllerMethods.Select(m => m.Name)
                .Distinct()
                .GroupJoin(
                    controllerMethods,
                    name => name,
                    netMethod => netMethod.Name,
                    (name, netMethods) => netMethods.First())
                .ToArray();

            //these are just the url fields
            var urlStringFields =
                   distinctControllerMethods.Select(m => GetUrlNavigateConstFieldDeclaration(m, controllerNetClass));
 
            //these are the route data objects           
            //var routeDataFields =
            //       distinctControllerMethods.Select(m => GetRouteDataFieldDeclaration(m, controllerNetClass));

            return urlStringFields.Cast<NetField>().Union(new [] { new NetFieldDeclaration
            {
                Name = "_baseUrl",
                FieldDeclarationType = NetFieldDeclarationType.ReadOnly,
                IsPublic = false,
                FieldType = new NetType
                {
                    Name = "string"
                }
            } }).ToArray(); //.Union(routeDataFields).ToList();
        }

        private NetMethod GetCsProxy(NetMethod controllerMethod, string controllerName)
        {
            var csProxy = new NetMethod
            {
                ReturnType = new NetType
                {
                    Name = "RestRequestAsyncHandle"
                },
                Name = controllerMethod.Name
            };
            <commit_msg>Methods on proxies should be public<commit_after>using System;
using System.Linq;
using System.Text.RegularExpressions;
using cstsd.Core.Extensions;
using cstsd.Core.Net;
using cstsd.Core.Ts;
using System.Collections.Generic;


namespace cstsd.TypeScript
{
    //TODO: rename to API generator?
    public class NetCsControllerConverter
    {
        public virtual NetClass GetControllerApiClientCsClass(NetClass controllerNetClass)
        {
            var controllerMethods = controllerNetClass
                .Methods
                .ToList();
            
            return new NetClass
            {
                Name = controllerNetClass.Name,
                Methods = controllerMethods
                    .Where(m => m.ReturnType.Name != "IActionResult")
                    .Where(m => m.IsPublic && m.Attributes.Any(a => a == "CsExport"))
                    .Select(a => GetCsProxy(a, controllerNetClass.Name))
                    .ToList(),
                Fields =  GetFields(controllerMethods, controllerNetClass),
                IsPublic = true,
                Constructor = GetConstructor(controllerNetClass)
            };
        }

        private static NetMethod GetConstructor(NetClass controllerNetClass)
        {
            return new NetMethod
            {
                IsPublic = true,
                Parameters = new[]
                {
                    new NetParameter
                    {
                        FieldType = new NetType
                        {
                            Name = "string"
                        },
                        Name = "baseUrl"
                    }
                },
                MethodBody = "_baseUrl = baseUrl;"
            };
        }

        private static string GetControllerName(string controllerName)
        {
            if (controllerName.EndsWith("Controller"))
                return controllerName.Substring(0, controllerName.Length - "Controller".Length);

            return controllerName;
        }

        private ICollection<NetField> GetFields(IReadOnlyCollection<NetMethod> controllerMethods, NetClass controllerNetClass)
        {
            var distinctControllerMethods = controllerMethods.Select(m => m.Name)
                .Distinct()
                .GroupJoin(
                    controllerMethods,
                    name => name,
                    netMethod => netMethod.Name,
                    (name, netMethods) => netMethods.First())
                .ToArray();

            //these are just the url fields
            var urlStringFields =
                   distinctControllerMethods.Select(m => GetUrlNavigateConstFieldDeclaration(m, controllerNetClass));
 
            //these are the route data objects           
            //var routeDataFields =
            //       distinctControllerMethods.Select(m => GetRouteDataFieldDeclaration(m, controllerNetClass));

            return urlStringFields.Cast<NetField>().Union(new [] { new NetFieldDeclaration
            {
                Name = "_baseUrl",
                FieldDeclarationType = NetFieldDeclarationType.ReadOnly,
                IsPublic = false,
                FieldType = new NetType
                {
                    Name = "string"
                }
            } }).ToArray(); //.Union(routeDataFields).ToList();
        }

        private NetMethod GetCsProxy(NetMethod controllerMethod, string controllerName)
        {
            var csProxy = new NetMethod
            {
                ReturnType = new NetType
                {
                    Name = "RestRequestAsyncHandle"
                },
                Name = controllerMethod.Name,
                IsPublic = true
            };
            
{'fraction_non_alphanumeric': 0.07571062912569042, 'fraction_numerical': 0.000269432843863667, 'mean_word_length': 1.372643016938319, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4820491', 'n_tokens_mistral': 1724, 'n_tokens_neox': 1599, 'n_words': 355}