/**
 * Create an encapsulated store.
 * @param {object} originalStore - Original store
 * @returns Encapsulated store.
 */
export function createEncapsulatedStore(originalStore) {
  const encapsulatedStore = {
    /**
     * Get the value of the key in the original store.
     * @param {string} action - The key name.
     * @returns {Proxy} The responsive value of the key in the original store.
     */
    dispatch(action) {
      if (typeof originalStore.dispatch === 'function') {
        return originalStore.dispatch(action);
      } else {
        throw new Error('Original store does not have a dispatch method.');
      }
    },

    /**
     * Get the value of the key in the original store state. Used for accessing.
     * @param {string} state - The key name.
     * @returns The value of the key in the original store. This return value is unresponsive.
     */
    getState(state) {
      if (typeof originalStore.getState === 'function') {
        // 返回原始状态的深拷贝，防止外部直接修改
        return JSON.parse(JSON.stringify(originalStore.getState(state)));
      } else {
        throw new Error('Original store does not have a getState method.');
      }
    },
  };

  return encapsulatedStore;
}