import { defineStore } from 'pinia'

const hasOwn = (target, key) => Object.prototype.hasOwnProperty.call(target, key)

// 控件运行态 Store：仅处理 UI 控件的显隐等运行时状态
const useMapUiStore = defineStore('mapUi', {
  state: () => ({
    controlVisibility: {},
  }),

  getters: {
    isControlVisible: (state) => (id) => {
      if (!id) return true
      const flag = state.controlVisibility[id]
      return flag !== false
    },
    hasControlOverride: (state) => (id) => {
      if (!id) return false
      return hasOwn(state.controlVisibility, id)
    },
  },

  actions: {
    setControlVisibility(id, visible) {
      if (!id) return
      this.controlVisibility[id] = visible !== false
    },

    resetControlVisibility(id) {
      if (!id) return
      delete this.controlVisibility[id]
    },

    clearControlVisibility() {
      this.controlVisibility = {}
    },
  },
})

export default useMapUiStore
