<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using TLI;
using static TsActivexGen.TSParameterType;
using static TLI.InvokeKinds;
using static TLI.TypeKinds;
using static TLI.TliVarType;
using System.Diagnostics;
using static System.StringComparison;
using static TsActivexGen.Functions;
using static TsActivexGen.tlibuilder.Functions;

namespace TsActivexGen.tlibuilder {
    public class TlbInf32Generator {
        private class ParameterizedSetterInfo {
            public TSSimpleType objectType { get; set; }
            public string propertyName { get; set; }
            public TSTupleType parameterTypes { get; set; }
            public TSSimpleType valueType { get; set; }
            public (string objectType, string propertyName, string parameterTypes, string valueType) Stringified => (GetTypeString(objectType, ""), propertyName, GetTypeString(parameterTypes, ""), GetTypeString(valueType, ""));
        }

        //unhandled values for VarType
        /*
VT_RESERVED	32768
VT_BYREF	16384
VT_CLSID	72
VT_CF	71
VT_BLOB_OBJECT	70
VT_STORED_OBJECT	69
VT_STREAMED_OBJECT	68
VT_STORAGE	67
VT_STREAM	66
VT_BLOB	65
VT_FILETIME	64
VT_RECORD	36
VT_USERDEFINED	29
VT_CARRAY	28
VT_SAFEARRAY	27
VT_PTR	26
VT_UNKNOWN	13
VT_ERROR	10
VT_NULL	1
 */

        private ITSType GetTypeName(VarTypeInfo vti, bool replaceVoidWithUndefined = false, bool isRest = false) {
            TSSimpleType ret;
            var splitValues = vti.VarType.SplitValues();
            if (splitValues.SequenceEqual(new[] { VT_EMPTY }) && vti.TypeInfo.TypeKind == TKIND_ALIAS && !vti.IsExternalType) {
                splitValues = vti.TypeInfo.ResolvedType.VarType.SplitValues();
            }
            var isArray = splitValues.ContainsAny(VT_VECTOR, VT_ARRAY);
            if (splitValues.ContainsAny(VT_I1, VT_I2, VT_I4, VT_I8, VT_R4, VT_R8, VT_UI1, VT_UI2, VT_UI4, VT_UI8, VT_CY, VT_DECIMAL, VT_INT, VT_UINT)) {
                ret = "number";
            } else if (splitValues.ContainsAny(VT_BSTR, VT_LPSTR, VT_LPWSTR)) {
                ret = "string";
            } else if (splitValues.ContainsAny(VT_BOOL)) {
                ret = "boolean";
            } else if (splitValues.ContainsAny(VT_VOID, VT_HRESULT)) {
                ret = replaceVoidWithUndefined ? "undefined" : "void";
            } else if (splitValues.ContainsAny(VT_DATE)) {
                ret = "VarDate";
            } else if (splitValues.ContainsAny(VT_EMPTY)) {
                var ti = vti.TypeInfo;
                ret = $"{ti.Parent.Name}.{ti.Name}";
                if (vti.IsExternalType && ResolveExternal) { AddTLI(vti.TypeLibInfoExternal, true); }
                interfaceToCoClassMapping.IfContainsKey(ret.FullName, val => ret = val.FirstOrDefault());
            } else if (splitValues.ContainsAny(VT_VARIANT, VT_DISPATCH, VT_UNKNOWN)) {
                ret = "any";
            } else {
                ret = "any";
            }
            if (!isArray) { return ret; }

            var safeArray = new TSGenericType() { Name = isRest ? "Array" : "SafeArray" };
            safeArray.Parameters.Add(ret);
            return safeArray;
        }

        private KeyValuePair<string, TSEnumDescription> ToTSEnumDescription(ConstantInfo c) {
            var ret = new TSEnumDescription();
            c.Members.Cast().Select(x => KVP(x.Name, AsString((object)x.Value))).AddRangeTo(ret.Members);
            ret.JsDoc.Add("", c.HelpString);
            return KVP($"{c.Parent.Name}.{c.Name}", ret);
        }

        private KeyValuePair<string, TSParameterDescription> ToTSParameterDescription(ParameterInfo p, bool isRest, List<KeyValuePair<string, string>> jsDoc) {
            var ret = new TSParameterDescription();
            var name = p.Name;
            ret.Type = GetTypeName(p.VarTypeInfo, true, isRest);
            if (isRest) {
                ret.ParameterType = Rest;
            } else if (p.Optional || p.Default) {
                ret.ParameterType = Optional;
            } else {
                ret.ParameterType = Standard;
            }
            if (p.Default) {
                var defaultValue = p.DefaultValue;
                if (defaultValue != null) {
                    var kvp = KVP("param", $"{ret.Type} [{name}={AsString(p.DefaultValue)}]");
                    if (!jsDoc.Contains(kvp)) { jsDoc.Add(kvp); }<commit_msg>Fix default value in jsDoc @param<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using TLI;
using static TsActivexGen.TSParameterType;
using static TLI.InvokeKinds;
using static TLI.TypeKinds;
using static TLI.TliVarType;
using System.Diagnostics;
using static System.StringComparison;
using static TsActivexGen.Functions;
using static TsActivexGen.tlibuilder.Functions;

namespace TsActivexGen.tlibuilder {
    public class TlbInf32Generator {
        private class ParameterizedSetterInfo {
            public TSSimpleType objectType { get; set; }
            public string propertyName { get; set; }
            public TSTupleType parameterTypes { get; set; }
            public TSSimpleType valueType { get; set; }
            public (string objectType, string propertyName, string parameterTypes, string valueType) Stringified => (GetTypeString(objectType, ""), propertyName, GetTypeString(parameterTypes, ""), GetTypeString(valueType, ""));
        }

        //unhandled values for VarType
        /*
VT_RESERVED	32768
VT_BYREF	16384
VT_CLSID	72
VT_CF	71
VT_BLOB_OBJECT	70
VT_STORED_OBJECT	69
VT_STREAMED_OBJECT	68
VT_STORAGE	67
VT_STREAM	66
VT_BLOB	65
VT_FILETIME	64
VT_RECORD	36
VT_USERDEFINED	29
VT_CARRAY	28
VT_SAFEARRAY	27
VT_PTR	26
VT_UNKNOWN	13
VT_ERROR	10
VT_NULL	1
 */

        private ITSType GetTypeName(VarTypeInfo vti, bool replaceVoidWithUndefined = false, bool isRest = false) {
            TSSimpleType ret;
            var splitValues = vti.VarType.SplitValues();
            if (splitValues.SequenceEqual(new[] { VT_EMPTY }) && vti.TypeInfo.TypeKind == TKIND_ALIAS && !vti.IsExternalType) {
                splitValues = vti.TypeInfo.ResolvedType.VarType.SplitValues();
            }
            var isArray = splitValues.ContainsAny(VT_VECTOR, VT_ARRAY);
            if (splitValues.ContainsAny(VT_I1, VT_I2, VT_I4, VT_I8, VT_R4, VT_R8, VT_UI1, VT_UI2, VT_UI4, VT_UI8, VT_CY, VT_DECIMAL, VT_INT, VT_UINT)) {
                ret = "number";
            } else if (splitValues.ContainsAny(VT_BSTR, VT_LPSTR, VT_LPWSTR)) {
                ret = "string";
            } else if (splitValues.ContainsAny(VT_BOOL)) {
                ret = "boolean";
            } else if (splitValues.ContainsAny(VT_VOID, VT_HRESULT)) {
                ret = replaceVoidWithUndefined ? "undefined" : "void";
            } else if (splitValues.ContainsAny(VT_DATE)) {
                ret = "VarDate";
            } else if (splitValues.ContainsAny(VT_EMPTY)) {
                var ti = vti.TypeInfo;
                ret = $"{ti.Parent.Name}.{ti.Name}";
                if (vti.IsExternalType && ResolveExternal) { AddTLI(vti.TypeLibInfoExternal, true); }
                interfaceToCoClassMapping.IfContainsKey(ret.FullName, val => ret = val.FirstOrDefault());
            } else if (splitValues.ContainsAny(VT_VARIANT, VT_DISPATCH, VT_UNKNOWN)) {
                ret = "any";
            } else {
                ret = "any";
            }
            if (!isArray) { return ret; }

            var safeArray = new TSGenericType() { Name = isRest ? "Array" : "SafeArray" };
            safeArray.Parameters.Add(ret);
            return safeArray;
        }

        private KeyValuePair<string, TSEnumDescription> ToTSEnumDescription(ConstantInfo c) {
            var ret = new TSEnumDescription();
            c.Members.Cast().Select(x => KVP(x.Name, AsString((object)x.Value))).AddRangeTo(ret.Members);
            ret.JsDoc.Add("", c.HelpString);
            return KVP($"{c.Parent.Name}.{c.Name}", ret);
        }

        private KeyValuePair<string, TSParameterDescription> ToTSParameterDescription(ParameterInfo p, bool isRest, List<KeyValuePair<string, string>> jsDoc) {
            var ret = new TSParameterDescription();
            var name = p.Name;
            ret.Type = GetTypeName(p.VarTypeInfo, true, isRest);
            if (isRest) {
                ret.ParameterType = Rest;
            } else if (p.Optional || p.Default) {
                ret.ParameterType = Optional;
            } else {
                ret.ParameterType = Standard;
            }
            if (p.Default) {
                var defaultValue = p.DefaultValue;
                if (defaultValue != null) {
                    var kvp = KVP("param", $"{name} [{name}={AsString(p.DefaultValue)}]");
                    if (!jsDoc.Contains(kvp)) { jsDoc.Add(kvp); }
{'fraction_non_alphanumeric': 0.10383386581469649, 'fraction_numerical': 0.012551346417161114, 'mean_word_length': 2.4198205228248146, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, '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': '340846', 'n_tokens_mistral': 2738, 'n_tokens_neox': 2653, 'n_words': 591}