 ;;; -*- lexical-binding: t -*-
+(load-library "map")
+(load-library "seq")
 
 ;; Set file, keyboard and terminal coding systems automatically
 (prefer-coding-system 'utf-8)

(use-package cmake-font-lock
  :hook (cmake-mode . cmake-font-lock-activate))

(defun find-js-format-style ()
  (let* ((package-json-dir
          (locate-dominating-file default-directory "package.json"))

         (package-json
          (if package-json-dir
              (json-read-file (concat
                               (expand-file-name package-json-dir)
                               "package.json"))
            nil))

         (devDependencies
          (if package-json
              (alist-get 'devDependencies package-json)
            nil))

         (formatter-styles
          '((prettier . prettier)
            (eslint   . eslint))))

    (autoload 'map-filter "map")
    (autoload 'map-contains-key "map")
    (or (cdr (car (map-filter
                   (lambda (package _)
                     (map-contains-key devDependencies package))
                   formatter-styles)))
        nil)))

(use-package prettier
  :commands prettier-mode
  :preface
  (defun setup-prettier ()
    (prettier-mode)
    (unless (key-binding "C-c f")
      (bind-key "C-c f" 'prettier-prettify (symbol-value (intern (concat (symbol-name major-mode) "-map"))))))
  :hook ((css-mode web-mode js-mode typescript-mode scss-mode yaml-mode markdown-mode) . setup-prettier))

;; Node
(add-hook 'js-mode-hook
          (lambda ()
            (use-package add-node-modules-path
              :config
              (add-node-modules-path)
              (let ((style (find-js-format-style)))
                (cond ((null style))
                      ((and (eq style 'eslint) (derived-mode-p 'js-mode))
                       (bind-key "C-c f" 'eslint-format-buffer (symbol-value (intern (concat (symbol-name major-mode) "-map"))))
                       (add-hook 'js-mode-hook 'eslint-format-on-save-mode)))))

            (use-package import-js
              :bind (:map js-mode-map
                          ("C-c t i"   . import-js-import)

(use-package flycheck-pos-tip
  :after (flycheck)
  :hook (flycheck-mode . flycheck-pos-tip-mode))

;; REST API
(use-package restclient
