import { ProSchemaValueEnumMap, ProSchemaValueEnumObj } from '@ant-design/pro-components';

/** 字典基类 */
export class Dict {
    /** 转换为数组 */
    toList(): DICTS.DictItem[] {
        let items: DICTS.DictItem[] = [];
        for (const key in this) {
            if (this.hasOwnProperty(key)) {
                items.push(this[key] as unknown as DICTS.DictItem);
            }
        }
        items = items.sort((a, b) => (a.sequence || 0) - (b.sequence || 0));
        return items;
    }
    /** 转换为选项 */
    toSelectOptions(): DICTS.SelectOptionItem[] {
        const items: DICTS.SelectOptionItem[] = [];
        for (const key in this) {
            if (this.hasOwnProperty(key)) {
                const item = this[key] as unknown as DICTS.DictItem;
                items.push({
                    key: key,
                    value: item.id,
                    label: item.name,
                });
            }
        }
        return items;
    }
    /** 转换为以ID为键的字典 */
    toIdKeyDict() {
        const dict: { [key: number]: DICTS.DictItem } = {};
        let id = 1;
        for (const key in this) {
            if (this.hasOwnProperty(key)) {
                const item = { id: id++, ...this[key] } as unknown as DICTS.DictItem;
                dict[item.id] = item;
            }
        }
        return dict;
    }
    /** 转换为以code为键的字典 */
    toCodeKeyDict() {
        const dict: { [key: string]: DICTS.DictItem } = {};
        for (const key in this) {
            if (this.hasOwnProperty(key)) {
                const item = { ...this[key] } as DICTS.DictItem;
                dict[item.code] = item;
            }
        }
        return dict;
    }
    /** 转换为ProTable中的valueEnum */
    toValueEnum(
        codeKey: boolean = false,
        excludes?: number[],
    ): ProSchemaValueEnumObj | ProSchemaValueEnumMap {
        const ve: ProSchemaValueEnumObj | ProSchemaValueEnumMap = {};
        for (const key in this) {
            if (this.hasOwnProperty(key)) {
                const n = this[key] as unknown as DICTS.DictItem;
                if (excludes?.includes(n.id)) {
                    continue;
                }

                const item = {
                    text: n?.name || '',
                    status: n?.status || '',
                };
                if (codeKey) {
                    ve[n.code] = item;
                } else {
                    ve[n.id] = item;
                }
            }
        }
        return ve;
    }
}

/** -----------------------------------------==类型==---------------------------------------- */


/** -----------------------------------------==状态==---------------------------------------- */



