import { defineStore } from "pinia";
import { ref } from "vue";

export const useDictStore = defineStore("dict", () => {
  const dict = ref<any[]>([]);

  // 获取字典
  function getDict(_key: string) {
    if (_key == null && _key == "") {
      return null;
    }
    try {
      dict.value.forEach(item => {
        if (item.key == _key) {
          return item.value;
        }
      });
    } catch (e) {
      return null;
    }
  }

  // 设置字典
  function setDict(_key: string, value: string | number) {
    if (!_key) return;
    dict.value.push({
      key: _key,
      value: value
    });
  }

  // 删除字典
  function removeDict(_key: string) {
    var bln = false;
    try {
      dict.value.forEach((item, index) => {
        if (item.key == _key) {
          dict.value.splice(index, 1);
          bln = true;
        }
      });
    } catch (e) {
      bln = false;
    }
    return bln;
  }

  // 清空字典
  function cleanDict() {
    dict.value = new Array();
  }
  // 初始字典
  function initDict() { }
  
  return {
    dict,
    getDict,
    setDict,
    removeDict,
    cleanDict,
    initDict,
  };
});
