import type { Rule } from "~/shared/models"
import type { ReduxFile } from "./state"

export const SET_ACTIVE_FILE_ID = "SET_ACTIVE_FILE_ID" as const
export const PATCH_FILES = "PATCH_FILES" as const
export const OPEN_FILE = "OPEN_FILE" as const
export const RENAME_FILE = "RENAME_FILE" as const
export const CHANGE_DIRTY = "CHANGE_DIRTY" as const
export const UPDATE_RULE = "UPDATE_RULE" as const
export const DELETE_RULE = "DELETE_RULE" as const
export const ADD_RULE = "ADD_RULE" as const
export const UPDATE_FILE_CODE = "UPDATE_FILE_CODE" as const
export const UPDATE_TAGS = "UPDATE_TAGS" as const
export const CLOSE_FILE = "CLOSE_FILE" as const

export const setActiveFileId = (id: string) => ({ type: SET_ACTIVE_FILE_ID, id })
export const openFile = (file: ReduxFile) => ({ type: OPEN_FILE, file })
export const patchFiles = (
  patches: (readonly [id: string, patch: Partial<ReduxFile>])[],
) => ({
  type: PATCH_FILES,
  patches,
})
export const renameFile = (fileId: string, name: string) => ({
  type: RENAME_FILE,
  fileId,
  name,
})
export const changeDirty = (fileId: string, dirty: boolean) => ({
  type: CHANGE_DIRTY,
  dirty,
  fileId,
})
export const updateRule = ({ index, rule }: { index: number; rule: Rule }) => ({
  type: UPDATE_RULE,
  index,
  rule,
})
export const deleteRule = (index: number) => ({ type: DELETE_RULE, index })
export const addRule = (rule: Rule) => ({ type: ADD_RULE, rule })
export const setFileCode = (code: string) => ({ type: UPDATE_FILE_CODE, code })
export const updateTags = (tags: readonly string[]) => ({ type: UPDATE_TAGS, tags })
export const closeFile = (fileId: string) => ({ type: CLOSE_FILE, fileId })
