// 假设 IEntity 接口定义如下
interface IEntity {
    ChangeToTable(): string[];
    ChangeFromTable(parms: any[]): void;
}

class CommonFunction {
    static enqualSign = "&&"; // 行分隔符
    static splitSign = "##"; // 列分隔符

    static ChangeDicToStr(dic: { [key: string]: string }): string {
        let result = '';
        if (dic) {
            let dicstr = '';
            for (const [key, value] of Object.entries(dic)) {
                dicstr += `${key}${this.enqualSign}${value}${this.splitSign}`;
            }
            if (dicstr.endsWith(this.splitSign)) {
                result = dicstr.slice(0, -this.splitSign.length);
            }
        }
        return result;
    }

    static ChangeStrToDic(str: string): { [key: string]: string } | null {
        const result: { [key: string]: string } = {};
        if (str) {
            const tmpstr = str.split(this.splitSign);
            for (const tmps of tmpstr) {
                const tmp = tmps.split(this.enqualSign);
                if (tmp.length !== 2) {
                    return null;
                }
                result[tmp[0]] = tmp[1];
            }
        }
        return result;
    }

    static ChangStrToArray(str: string): string[] {
        if (str) {
            return str.split(this.splitSign);
        }
        return [];
    }

    static ChangArrayToStr(stringarray: string[]): string {
        let result = '';
        if (stringarray) {
            let dicstr = '';
            for (const tmp of stringarray) {
                dicstr += `${tmp}${this.splitSign}`;
            }
            if (dicstr.endsWith(this.splitSign)) {
                result = dicstr.slice(0, -this.splitSign.length);
            }
        }
        return result;
    }

    static ChangeDicToTable(dic: { [key: string]: string }): any[] {
        const result: any[] = [];
        if (dic) {
            for (const [key, value] of Object.entries(dic)) {
                result.push({ Key: key, Value: value });
            }
        }
        return result;
    }

    static ChangeTableToDic(table: any[]): { [key: string]: string } | null {
        const result: { [key: string]: string } = {};
        if (table) {
            for (const tmp of table) {
                const itemArray = Object.values(tmp);
                if (itemArray.length !== 2) {
                    return null;
                }
                result[itemArray[0].toString()] = itemArray[1].toString();
            }
        }
        return result;
    }

    static ChangeEntityToTable(entitys: IEntity[]): any[] {
        const result: any[] = [];
        if (entitys) {
            for (const tmpentity of entitys) {
                if (tmpentity) {
                    const tmpvalues = tmpentity.ChangeToTable();
                    if (tmpvalues) {
                        if (result.length === 0) {
                            for (let i = 1; i <= tmpvalues.length; i++) {
                                result.push({});
                            }
                        }
                        for (let i = 0; i < tmpvalues.length; i++) {
                            result[i][`column${i + 1}`] = tmpvalues[i];
                        }
                    }
                }
            }
        }
        return result;
    }

    static ChangeTableToEntitys<T extends IEntity>(table: any[]): T[] {
        const result: T[] = [];
        if (table) {
            for (const list of table) {
                const tmp = new (class implements IEntity {
                    ChangeToTable(): string[] {
                        throw new Error("Method not implemented.");
                    }
                    ChangeFromTable(parms: any[]): void {
                        throw new Error("Method not implemented.");
                    }
                })() as T;
                tmp.ChangeFromTable(Object.values(list));
                result.push(tmp);
            }
        }
        return result;
    }
}

export default CommonFunction;